I have a simple but annoying problem and I don't understand the reason. I need to upload a simple csv file with php. Here's my code:
index.php:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" value="submit" />
</form>
upload.php:
$upfile = "csv/".$_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $upfile);
No errors whatsoever, but the file is still not uploaded on the server.
UPDATE: print_r($_FILES) output:
Array ( [fileUpload] => Array ( [name] => file1.csv [type] => text/comma-separated-values [tmp_name] => /var/tmp/php6YZ4Bt [error] => 0 [size] => 45 ) )
Things to check
1) make sure your directory has permissions set to 755
2) check your path to see if it is correct.
3) make sure your post_max_size is the proper size.
4) make sure all your errors are turned on.
This can be done in the php script by using this code:
error_reporting(E_ALL); // or E_STRICT
ini_set("display_errors",1);
5) Increase your memory limit to see if the script is using more memory than previously allocated..
ini_set("memory_limit","1024M");
Related
This is my first post here to stackoverflow. I searched for other solutions to this problem on this site but was unable to find any solution. I even had an extensive session with GoDaddy support that was unsuccessful.
Here is my code, which is slighty modified from the PHP reference:
<html>
<body>
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
if (isset($_POST['submit'])){
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$tmpfile = $_FILES['userfile']['tmp_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 'File name: '.$uploadfile."\r\n";
echo 'Temp file: '.$tmpfile."\r\n\r\n";
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
}
}
?>
<form enctype="multipart/form-data" action="#" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" name="submit" id="submit" value="Send File" />
</form>
</body>
</html>
The output when I upload a file is this:
Warning: move_uploaded_file(uploads/Bender.jpg): failed to open stream: No such file or directory in /home/nflaum/public_html/_working/file_upload.php on line 15
Warning: move_uploaded_file(): Unable to move '/tmp/phpWmeSDx' to 'uploads/Bender.jpg' in /home/nflaum/public_html/_working/file_upload.php on line 15
Possible file upload attack! File name: uploads/Bender.jpg Temp file: /tmp/phpWmeSDx
Here is some more debugging info:Array (
[userfile] => Array
(
[name] => Bender.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpWmeSDx
[error] => 0
[size] => 91463
)
)
Here are things I have already tried:
Created my own php.ini file and tried every combination of upload_tmp_dir imaginable (verifying with phpinfo), both on my own and with a GoDaddy rep. Verified file_uploads is on and all settings are ok.
Verified that the folders had 777 permissions for every combination tried.
Tried renaming .htaccess in case some routing configuration was causing an issue.
Connected to SSH and checked the root /tmp folder to see if it was there (GoDaddy sets home folder to /home/)
None of it resolved the issue. It looks like PHP thinks the file is being uploaded correctly, yet it's nowhere to be found. I'm hoping one of the geniuses on this forum is able to help me see something I've missed.
I think the path is not correct from the script current position. Did you check that? Is the folder "uploads" in the same directory of the file_upload.php script?
I'm having users upload .xls, .xlsx, and .csv files to my website. From there I want to save them to the folder /upload/ in the same directory as the handler. I also want the name to remain constant. I have developed a code from what I have seen across the internet. Upon submission, the page turns white and no file is uploaded to the folder.
Form:
<form name="file" enctype="multipart/form-data" action="update_handler.php" method="post" >
<b>Accepted File Extensions: .csv .xls </b><br>
Please save your excel table as one of the <br>accepted file extensions prior to uploading.
<br><br>
<u>Upload File:</u> <input type="file" name="file"><br>
<input type="submit" value="Submit">
</form>
Handler:
<?php
$uploadDir = '/upload/';
$fileName = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
?>
You named your form file <form name="file"
Where it should be for your input <input type="file" name="file"> <= which am sure read something else, since it was edited. Both form and the input hold the same named attribute. Remove the one for the form or give it another name if it is required for something else.
Also, David pointed out another issue which I did think of earlier, but failed to mention after. Since I took your comment about the "face palm" had fixed everything. I would've commented, but did not.
I was going to suggest that actually, about your use of /upload/ instead of upload/ if running your script from the root, but failed to mention it earlier.
David seems to have deleted his answer.
Plus; you may need to use ../upload/ or something similar, depending on the script's execution location and make sure proper write permissions are set.
I have the following code that attempts to take a users form input of a file, and upload it to the webserver.
This code does work on a Apache server, however I'm now trying to get the same code working on my Windows IIS 6 web server, which has PHP (Version 5.2.3) installed and working. I have set the PHP.INI file so that
file_uploads = On
upload_tmp_dir = "C:\Temp"
My form is
<form method="POST" action="do_upload.php" enctype="multipart/form-data">
<input type="file" name="img1" size="30">
<input type="submit" name="BtnUpload" value="Click To Upload Now">
</form>
My PHP code to do the upload is
$abpath = "C:\MyWebs\Website1\httdocs\images";
#copy($img1, "$abpath/$img1_name") or $log .= "Couldn't copy image 1 to server";
if (file_exists("$abpath/$img1_name"))
{
$log .= "File 1 was uploaded";
}
else
{
$log .= "File 1 is not an image";
}
For some reason when I check the value of $img1 e.g echo $img1; it is empty. Therefore I tried to get the file using $_FILES['img1']['name']. This worked fine, but still I couldn't upload any files
Any ideas why this is happening.
Your code should be:
move_uploaded_file($_FILES['img1']['tmp_name'], "$abpath/$img1_name");
Don't copy() uploaded files. There are a few edge cases where an uploaded file can be tampered with, which is why move_uploaded_file() exists - it checks for those particular types of tampering.
As well, be VERY careful with how you create your filenames when processing the upload. If you directly use ANYTHING provided in $_FILES as part of the destination path/name for the file, you are opening bad security holes on your server, and a malicious user can exploit that to scribble a file anywhere they want on your server.
After an upload, the print_r output for the image field is as follows
Array
(
[name] => foo.png
[type] => image/png
[tmp_name] => /tmp/php63EvNo
[error] => 0
[size] => 19115
)
Since the error is zero, and the filesize is non-zero I assume that the upload is successful.
A subsequent call to move uploaded file fails: move_uploaded_file(...): failed to open stream: Permission denied
Upon inspecting /tmp, the file named in tmp_name is not there.
What causes this behaviour/ how to rectify?
Thanks!
Extra info:
LAMP stack, running PHP5, CakePHP 2.0
The form:
php/ cake code:
echo $form->input('Foo.image', array('type' => 'file'));
html that is rendered:
<input type="file" name="data[Foo][image]" id="FooImage"/>
The temporary uploaded files in /tmp are deleted at the end of the request -- they are not kept around long-term. You have to use move_uploaded_file during the request that received an upload if you care about the file.
Most likely a permissions issue. Also consider using the cakePHP folder variables. The upload component I have looks like this:
$file = $this->request->data['Client']['image'];
$filename = $file['tmp_name'];
$filePath = WWW_ROOT . DS . 'files' . DS . $file['name'];
if(move_uploaded_file($filename, $filePath))
return '/files/'.$file['name']; // saves location of uploaded file
I have a little problem with uploading multiple files in PHP ,
i have this html form:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="myfile[]" />
<input type="submit" />
</form>
and this is the upload.php :
<?php print_r( $_FILES ); ?>
when i'm sending a file it show me this:
Array
(
[myfile] => Array
(
[name] => Array
(
[0] => Krw_Qe4QKmI.mp3
)
[type] => Array
(
[0] =>
)
[tmp_name] => Array
(
[0] =>
)
[error] => Array
(
[0] => 1
)
[size] => Array
(
[0] => 0
)
)
)
so far so good.
the problem starts when i upgrade my form to this one :
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="myfile[]" />
<input type="file" name="myfile[]" />
<input type="submit" />
</form>
now , when i send 2 files , it show me this :
Array
(
)
so , what's the problem here?
thank you , Mor.
I would bet that you exceeded post_max_size and PHP just ignored the uploaded files.
It's 8MB by default. If you try to upload one 5MB file everything will work. If you try to upload 2 5MB files, it exceeeds 8MB and PHP ignores posted data.
Try increasing the value of post_max_size in your php.ini.
A lot of suggestions here. I'll give it a go. This is based on #Pekka 's comment.
I see you're testing with mp3s, which probably exceed PHP upload limit. This is because in your first example, you actually have an upload error code 1: The uploaded file exceeds the upload_max_filesize directive in php.ini.. So even your fist upload didn't work. A successful upload always has 0 as the error code.
Modify you php.ini with upload_max_filesize = 10M (or 20M, or 300M; careful about that M - which means megabytes - as omitted, brings alot of headache.
I suggest testing with smaller files, as I see you have a limit of 2M for uploading.
Further reading.
Check your max_file_uploads setting -- is it more than 1?
echo ini_get('max_file_uploads');
The php.ini file must have something like this:
;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;
; Whether to allow HTTP file uploads.
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =
; Maximum allowed size for uploaded files.
upload_max_filesize = 50M
; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
Change the values of upload_max_filesize and then restart the server
To do multiple files at once, try giving an index like this:
<? For ( $count = 0; count < SOME_MAXIMUM; ++$count; ): ?>
<input type="file" name="myfile[<? Echo $count; ?>]" />
<? endfor; ?>
I had the same problem.. All my efforts were in vain, but finally I found a pretty good note at the PHP Manual. It's simple but suited me perfectly...
"Multiple upload might not work if you use a table for displaying your
form inputs when <form> element is inside the <table> element. In this
case only the first file will be uploaded.
Put the <form> element outside the element to get it to work."
Follow this link for the full note, there is a function to rearrange the multiple upload file array in a easy-to-use manner.
http://php.net/manual/en/features.file-upload.multiple.php
The Problem is your name="myfile[]" attribute on your input-Element.
You cant referer later in PHP to your file, if you haven't an identifier for it. The PHP-Documentation gives you the same hint:
http://www.php.net/manual/en/features.file-upload.multiple.php
Multiple files can be uploaded using different name for input.
So change the name to "myfile1" and "myfile2" (or some better name ;)) should solve your problem.