Can't upload files with PHP - 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?

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

Upload files through PHP not working

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...

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

I can not upload a file via php

I am trying to upload an image into a database via php, but I face the follow problem.
Upload failed
Here is some more debugging info:
Notice: Undefined index: filetoUpload in C:\Users\Konstantina\Desktop\Upload.php on line 39
Upload.php code :
$uploaddir = 'upload/';
if(!file_exists($uploaddir)){
if(mkdir($uploaddir,0777,true)){}else{echo "fail to create folder";}
}
$uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']);
if (move_uploaded_file($_FILES['fileToUpload']['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:';
echo($_FILES['filetoUpload']['errors']);
Insert.html code :
<form action="Upload.php" method="post" enctype="multipart/form-data" name="myform" id="myform">
<label > Select image </label>
<input type="file" name="fileToUpload" >
<input id="upload" type="submit" name="submit" value="Upload">
</form>
I have turn on file_uploads=On
You're outputting the wrong index to retrieve the error:
echo($_FILES['filetoUpload']['errors']);
should be:
echo($_FILES['fileToUpload']['errors']);
Afther this, we can know what the error is based on the manual:
http://php.net/manual/es/features.file-upload.errors.php
i think you use full path of upload directory
$uploaddir = 'upload/';
use like
$uploaddir = '/var/www/upload/';
also you have
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
echo($_FILES['filetoUpload']['errors']); // here to is small t while in other you have To

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