Upload files through PHP not working - php

I am trying to upload a file using a PHP form. I have looked through the solutions suggested here but I still can't get it to work.
My html form is
<form enctype="multipart/form-data" action="customer_details.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
and then my PHP part
$uploaddir = 'temp_files/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$tmp_file = $_FILES['userfile']['tmp_name'];
echo 'Tmp file is: ' . $tmp_file;
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
//die();
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
The results that I am getting from the debugging info are as follows:
[userfile] => Array
(
[name] => EasyCount Template (1).xlsx
[type] => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
[tmp_name] => /tmp/phpcfn8tp
[error] => 0
[size] => 10012
)
I have created the tmp folder in all possible locations I can think off, yet I still can't figure out what is stopping the file from being uploaded. I've been trying to find the problem for a while now but I just can't see it. The customer_details.php is in a platformDev folder on the server, I have created a tmp folder in here as well.

Problem solved... Tip for anyone else coming across this problem in the future - Check your folder permissions and make sure you can write to them...

Related

php File upload to directory failure, tmp_name and type not being generated

I am using apache 2.4 and PHP 7.3,
Here I am failing to upload images to the directory,
PROBLEM: tmp_name and type not generated.
tried functions:move_uploaded_file,copy
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
echo $_FILES['userfile']['tmp_name'];
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
echo $_FILES["userfile"]["error"];
print "</pre>";
?>
OUTPUT:
Upload failed
Here is some more debugging info: Array
(
[userfile] => Array
(
[name] => hope.png
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)
)
2

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.
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']);

Simple file upload - how to add .jpg extension

I am creating a very simple admin panel and I want to have posibility of uploading a file, but this is gonna be only one file with static name. I have this code:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename(uploadedfile.jpg);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
And this works, but file is being uploaded with name "uploadedfilejpg" so there is no extension. How to fix this to add extension? I only want to upload jpg files and overwrite old file.
You've got an error in your PHP script on line 4. You've got basename(uploadedfile.jpg), which is an error. I assume you meant to write basename('uploadedfile.jpg'), but since using basename on a file name just returns the file name (thanks to #CBroe for noticing this), you might as well just use 'uploadedfile.jpg'.
It should look like this:
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . 'uploadedfile.jpg';
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
replace this line :
$uploadfile = $uploaddir . basename('uploadedfile.jpg');
with:
$uploadfile = $uploaddir . basename('uploadedfile.jpg'). '.jpg';

Can't upload files with PHP

i am going to use a simple php script to upload images to the specified folder but i got this error after sending file:
Warning: move_uploaded_file(): Unable to move '/tmp/phpbY8z4A' to 'http://www.yapi-dekorasyon.net/images/Exactly-Sport-According-to-Zodiac.jpg' in /home/pyapitj2/public_html/upload/upload.php on line 8
Upload failed
Here is some more debugging info:Array
(
[userfile] => Array
(
[name] => Exactly-Sport-According-to-Zodiac.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpbY8z4A
[error] => 0
[size] => 119217
)
)
index.php;
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
upload.php;
$uploaddir = 'http://www.yapi-dekorasyon.net/images/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
As i tried many other scripts for uploading files to the site i got similar errors. How to solve this problem?
Thanks...
You are attempting to upload the file to a URL folder, instead of a Path such as /var/www/vhosts/domain.com/httpdocs/uploads/
Could it be a server problem instead of script?

PHP upload not working

I've been trying to get my PHP upload script working, the HTML side seems to work but the PHP keeps returning a failed result. I am using iPage hosting. Here is my script:
<?php
if(isset($_FILES['userfile'])){
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
} else {
?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
<?php
}
?>
Comment to answer to close the question, since that's what the issue was, both.
Check to see if the folder is writeable. If it is, then try a relative path instead of what you're using now.
I.e.: $uploaddir = 'uploads/';
Make sure the upload directory has the correct permissions - Writable and Proper Owner
You will run into issues when the file size is over 512000, you should check the size of the file using the $_FILE array and return an error message. Just a suggestion as you have already closed this.
// Error Checking Extended
if($_FILES['userfile']['error'] == 2) {
echo "You've exceeded the maximum file upload size of 512kb.";
return false;
}

Categories