Sam
Sam

Reputation: 143

Uploading an image to my server

So I want to allow users to upload an image and want to store that image in my server. (Using Ubuntu and Apache) so server path is (/var/www/html/)

My HTML code is:

<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="uploadimages.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form> 
</body>
</html>

and my uploadimages.php file code is:

<?php
$uploaddir = '/var/www/html/images';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>

When I click the bottom send I get the following message:

File is valid, and was successfully uploaded. Here is some more debugging info:Array ( [userfile] => Array ( [name] => thisisthepictureIupload.png [type] => image/png [tmp_name] => /tmp/phpIUEUjE [error] => 0 [size] => 4596 ) )

But when I go to the folder: /var/www/html/images the image is not there.

Any help would be highly appreciated.

Upvotes: 0

Views: 91

Answers (1)

Adam Purdie
Adam Purdie

Reputation: 502

I think you might be missing the / in your file path:

 $uploaddir = '/var/www/html/images';
 $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

should probably be

$uploaddir = '/var/www/html/images';
$uploadfile = $uploaddir . '/' . basename($_FILES['userfile']['name']);

Upvotes: 2

Related Questions