I am new to PHP and I'm trying to make an upload script. But it doesn't work completely.
The thing that doesn't work is that when I have uploaded the photo it doesn't store the photo in the folder "uploads". (The folder location is: Applications > MAMP > htdocs > Marjolein)
Also I want to show the photo that has been uploaded in the browser, but this also doesn't work.
I work with a Mac and use MAMP to run my php code. Can you please help me so I can show the picture in the browser and that it will be stored in the folder "uploads"?
The code I have is:
uploader.php
<style>
.sucess{
color:#088A08;
}
.error{
color:red;
}
</style>
<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("://Applications/MAMP/htdocs/Marjolein/uploads/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
<?php
if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!='')
{
?>
<p><img src="uploads/<?php echo $_REQUEST['show_image'];?>" /></p>
<?php
}
?>
uploadform.html
<html>
<body>
<form action="uploader.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
The webbrowser shows after I click on the submit button:
Upload: 0_8caab_996cc75d_orig.jpg
Type: image/jpeg
Size: 538.166992188 kB
Temp file: /Applications/MAMP/tmp/php/phptpCA8B
Stored in: ://Applications/MAMP/htdocs/Marjolein/uploads/0_8caab_996cc75d_orig.jpg