I'm trying to upload some Excel files on the server, but unfortunately is doesn't work for some files.
my html code looks like this:
<form action="my_upload.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>
php file looks like this
echo '<pre>';
print_r($_FILES);
echo '</pre>';
and the output
Array
(
[file] => Array
(
[name] => speeds.xls
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
It is not file path, naming, size or rights problem but it seems that is a file content problem. I'm saying that is a file content problem because the upload succeeds in some cases. Also if I re-save the Excel file that dind't work in the first place, then the upload file succeed.
How can I solve this problem? Why $_FILES['file']['error'] = 1. How to prevent that?
*Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.*
The error code indicates that the file you're trying to upload is larger than the file upload size limit set in php.ini
You can get a full list of these codes here
You need to change the limit in php.ini to allow larger uploads. If you're on a hosted system you might need to check with them hoe to do this: arrangements vary from host to host.
Related
When I upload a file in PHP I am getting the wrong type for xlsx files.
It is telling me the type is a file name
Array (
[name] => report.xlsx
[type] => /folder/files/report/april_2013.xlsx
[tmp_name] => E:\path\to\temp\folder\phpCD1B.tmp
[error] => 0
[size] => 12433
)
Any ideas?
Hi
The code I am using is
view/display file
<form enctype="multipart/form-data" method="POST" action="update_comp.php" name="myform2">
<label>Upload Files ( PDF,CSV or EXCEL only ) </label>
<input type="file" name="file">
</form>
File update_comp.php
include ("filesFunctions.php");
if(isset($_FILES["file"])){
$file = $_FILES["file"];
print_r($file);exit;
$upload = new filesFunctions();
$id = $upload->uploadFiles($file,'dairy');
}
the print_r() statement prints
Array (
[name] => report.xlsx
[type] => /folder/files/report/april_2013.xlsx
[tmp_name] => E:\folder\to\temp\php5ACD.tmp
[error] => 0
[size] => 12433
)
Please note this only happens to xlsx files all other files work fine
The type key provides the MIME type of the file but this information is provided by the browser. As such:
This mime type is however not checked on the PHP side and therefore
don't take its value for granted.
However, it's worth nothing that the $_FILES superglobal is by no means read-only. Like other superglobals, your PHP code can overwrite it:
$_FILES['file']['type'] = 'Lorem ipsum dolor sit amet';
Assuming that you aren't fiddling with $_FILES, the first obvious troubleshooting step is to fire your browser's debugger tools and inspect the POST data it sends. If that's the source of the string, you can forget about PHP.
I don't know what your browser is. In Google Chrome (in other browsers similar instructions apply) you must open the upload form, hit F12 and then click on "Network":
When you pick a file and submit the form, you'll see the request:
Expand the node and you'll see the data (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet in this case):
This MIME type is normally configured by the Microsoft Office installer. If yours contains /folder/files/report/april_2013.xlsx you might need to repair or reinstall.
Just totally wrong file? Name: report.xlsx
And after that youn say april_2013.xlsx like #h2ooooooo said.
I think you just copied the code from another file.
I'm creating a form to upload video file, but I have a strange error.
My form is like this:
<div style="color : red;"><?php echo $error;?></div>
<?php echo form_open_multipart('data_center/ajout_ressource/formulaire_video'); ?>
//various input text fields...
<input type="file" name="video">
<input type="submit" value="Ajouter cette ressource" />
</form>±
My method to get upload is (it's inside something to check if the rest of the form is ok):
$dir = './assets/video/';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'avi|flv|wmv|mpeg|mp3|mp4';
$config['max_size'] = '102400';
$this->load->library('upload',$config);
if ( ! $this->upload->do_upload('video')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('data_center/ajout_video', $error);
} else {
redirect('data_center/data_center/','refresh');
}
Concerning mime types, I think it's ok:
'mp4' => 'video/mp4',
'wmv' => array('video/wmv', 'video/x-ms-wmv', 'flv-application/octet-stream', 'application/octet-stream'),
'flv' => array('video/flv', 'video/x-flv', 'flv-application/octet-stream', 'application/octet-stream'),
'avi' => 'video/x-msvideo',
When I try to upload a mp4 file it automatically redirect me (although I'm not sure it's a redirection) to the form, unfilled and no error message, even if I comment the lines in case of upload failure not to load the view again or if the rest of the form is uncorrect (it should not attempt to upload the video and load the form pre-filled).
Whereas for example, if I put the wrong type of file, I can see the error message ('The filetype you are attempting to upload is not allowed' for example), or if the form is wrong I can see the error of the form.
Finally, I must add that I modified php.ini to authorize such big files, and that I did pretty much the same thing with a form for pictures (jpg,jpeg,png...) which works perfectly but also redirect me to an unfilled form if I try to upload a .mp4 file.
Edit: I just downloaded an flv video to try, and it uploaded perfectly fine, but its size was less than 8mb (default config), and a 40mb flv file had the same problem as the mp4 file, although I changed my config in php.ini for 100mb
Efectively you have to change two parameters in PHP ini file
post_max_size = 100M
upload_max_filesize = 100M
But you may also want to change the apache abuse protection parameter (100M)
LimitRequestBody 1073741824
And by another hand, PHP have a time limit too of 30 sec per script, so your script will die at 30 seconds of running.
You may also want to increase the time to be sure your script does not die meanwhile you are uploading, copying etc,
set_time_limit(600); // 10 minutos execution
I'm reading this line in Linux. However, when I'm echoing this in the browser, nothing shows up. Is there something wrong with how I used the echo line?
// relevant code snippets
$mypic = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
/*$finfo=finfo_open(FILEINFO_MIME_TYPE);
$type=finfo_file($finfo,$temp);
finfo_close($finfo);*/
echo "<pre>"; print_r($_FILES);
echo $mypic."<br />";
echo $type."<br />";
echo $_FILES['upload']['error']."<br />";
echo var_dump($type)."<br />";
If you suspect something is wrong with how I'm handling file inputs in another file, I've included that php file in this link.
<form ENCTYPE="multipart/form-data" method="post" action="insert.php">
Name: <input type="text" name="name" maxlength="15" /><br />
Email: <input type="text" name="email" maxlength="30" /><br />
Password: <input type="password" name="password" maxlength="15" /><br />
Confirm Password: <input type="password" name="cpassword" maxlength="15" /><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
Choose your picture: <input type="file" name="upload"><p>
<input type="submit" name="submit" value="Register" /><br />
<p>
<center><h3><?php include("links.php"); ?></h3></center>
</form>
Here is the printout that I'm seeing:
Array (
[upload] => Array
(
[name] => protest.jpg
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)
) protest.jpg
2 string(0) ""
------------------Update as of 9:40 p.m. May 5, 2012-------------------------
I tried an icon and found no problems other than permissions settings (I think I can solve this on my own for the time being). However, I'm still stuck on setting the file size. I followed Peter Stuart's instructions and got the following printout:
Apparently, the file size limits in these two settings are more than enough to handle the original images I had (which are under 200 kb). I don't know what more I can do in this case.
The file type is empty for the same reason that the filesize is 0 and the error is 2.
From Error Messages Explained:
UPLOAD_ERR_FORM_SIZE Value: 2; The uploaded file exceeds the
MAX_FILE_SIZE directive that was specified in the HTML form.
You have your max size set to 10000, which is in bytes, so that's roughly 10Kb. If it's a photo taken on any modern digital cam (over 4mgpx) it will probably need to be at least ten times that size. Just leave out the max size for now until you get a rough average of the image size people are submitting. PHP has a max upload size of its own to avoid tying up the line for too long.
To avoid issues like this in the future (not knowing if the file upload was successul), you probably want to wrap your code in something like:
$upload_error[0] = "AOK";
$upload_error[1] = "Server says: File too big!";
$upload_error[2] = "Browser says: File too big!";
$upload_error[3] = "File upload didn't finish.";
$upload_error[4] = "Ummm.... You forgot the file.";
$upload_error[6] = "Oops. Webmaster needs to fix something.";
$upload_error[7] = "Server says: I'm stuffed. Email webmaster.";
$upload_error[8] = "Server says: Not gonna do it. Webmaster needs to fix something.";
if($_FILES['upload']['error'] == 0) {
//do your thing and move it to the database.
} else {
$error_num = $_FILES['upload']['error'];
echo $upload_error[$error_num];
}
I would check your PHP upload size limit in your PHP ini file. It can cause adverse problems :)
If you create or go into your php.ini file and make sure the settings are as follows:
upload_max_filesize = 5M
post_max_size = 5M
The order of settings are also matter, and it should be like
upload_max_filesize = 5M
post_max_size = 5M
And I always got maximum size error when post_max_size place before upload_max_filesize.
This problem has 7 years but I stopped with it without finding a clear procedure to understand it. This is How I controlled it:
The sintom was that I could upload SMALL images (less than 2MB) BUT not bigger than 2MB. It's important to identify perfectly the ERROR. In this case "UPLOAD_ERR_FORM_SIZE:" The sintom in the DEBUG was that $_FILES["image"]["type"] = "", (ridiculous) and I knew that was a .JPG image for sure.
SOLUTION: Using XAMPP, STOP it. Configure php.ini, go to "upload_max_filesize=2M" which means that the file you try to upload has a limit of 2 Megabytes, So you Will change it to (for example) 3M. After that, I started again XAMPP, and proceeded to upload an image of 2.5 MB, and was successful.
Im sorry but my status can't show images of configuration in this comment.
Right now I'm trying to write a script that will only accept certain audio files for upload to a server.
However, some MIME types are being returned as null.
Here is some code fragments:
PHP:
$allowedExt=array('audio/mp4a-latm');
if(isset($_POST))
{
print_r($_FILES);
//ToDo: Limit by File Size
if(in_array($_FILES["fileUpload"]["type"],$allowedExt)){
echo "file type found";
}
else
echo "wrong file type";
}
HTML:
<form action="php/FileUpload.php" method="POST" enctype="multipart/form-data">
Choose a file: <input name="fileUpload" type="file" /><br />
<input type="submit" value="Upload" />
</form>
The result of the above is:
Array ( [fileUpload] => Array ( [name] => 02 Helix Nebula.m4a [type] => [tmp_name] => <removed for space...>))
wrong file type
From what I understand, type should return the file's MIME type. In this case 'audio/mp4a-latm' for a .m4a file.
If php is properly returning null for .m4a files, then what would be the best approach to ensure I'm actually dealing with audio files? Is there anything more definite than just parsing for the file extensions? (ensure someone hasn't change the extension of say a text document)
The MIME element comes from the browser, which means it can be manipulated and thus is not trustworthy.
Either check the extension, or if you really want, parse the first few bytes of the file to make sure it's what is expected.
$_FILES['userfile']['type'] - The mime type of the file, if the browser provided this information.
http://www.php.net/manual/en/features.file-upload.post-method.php
That's why this method doesn't work well. You should compare file extension grabbed from
$_FILES['userfile']['name']
with acceptable extensions array (which you should create by yourself)
If you use php 5.3 or higher you can activate the php file info extension by escaping this line in your php.ini:
extension=php_fileinfo.dll
Then you can get your mime type from the file in php like this:
$pathToFile = 'my/path/to/file.ext';
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($fileInfo, $pathToFile);
finfo_close($fileInfo);
This is more reliable than using what the browser sends you in the $_FILES array from your POST request.
With Apache/PHP5, is it possible to get the contents of an uploaded file directly without having it written to the file system?
Not much on Google about this, but it appears that files are always written to a temporary directory once they are uploaded.
Not sure i understand you, but i will try to answer.
http://www.w3schools.com/PHP/php_file_upload.asp
What you can learn here is that every file is uploaded to php's temp directory. Then it is up to your script to move/copy that file to some permanent web accessible directory, because file that was uploaded to php's temp dir is deleted after the script end executing.
On Linux you can create filesystem partitions in memory. If you can ensure that the uploaded file is written to the partition in memory, if will be stored in memory but act as if it were stored in the filesystem, making both Apache/PHP5 and you happy.
The problem is that any solution which writes files to memory rather than to the filesystem requires severe limitations on the size and quantity of the files. You will see a speed boost by avoiding writing to disk, but if you use enough memory to push other data into the pagefile or swap, your "optimization" will become counterproductive very quickly.
You can:
<?php
if (!empty($_FILES))
{
echo "<pre>";
print_r($_FILES);
echo file_get_contents($_FILES['file']['tmp_name']);
echo "</pre>";
}
?>
<form id="myForm" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Send" />
</form>
Output:
Array
(
[file] => Array
(
[name] => mytextfile.txt
[type] => text/plain
[tmp_name] => D:\PHP\wamp\tmp\php1283.tmp
[error] => 0
[size] => 1473
)
)
My text file My text file My text file My text file My text file My text file
My text file My text file My text file My text file
My text file My text file My text file My text file My text file My text file
My text file My text file My text file
My text file My text file My text file My text file My text file
I don't know if it's restricted by some php.ini variable.