I’m encountering a problem with file upload in PHP. I’m using Dropzone.js which calls a PHP script.
The PHP environment runs on Docker, and the PHP version is : 7.2.28
When I upload an image with Firefox 72 on Mac OSX, I get this in $_FILES :
Array\n(\n [file] => Array\n (\n [name] => image.png\n [type] => \n [tmp_name] => \n [error] => 3\n [size] => 0\n )\n\n)\n,
According to the documentation: error 3 means UPLOAD_ERR_PARTIAL.
The problem happens only with Firefox on Mac OSX, with PNG images with this specific size (158ko).
Other file size (even tiny or big files), other browsers, other file types, or other operating systems work fine.
The docker image runs on 3 different servers, and the problem happens on each installation.
I tried some solutions that I read on the internet, but none of them worked:
php_sapi_name() returns apache2handler
I tried to add 'Accept-Ranges: none' to my php file.
Do you have a clue on what might happen?
Thanks in advance,
Related
I ran into an issue earlier today with uploading a .csv file created from a Mac vs a PC through Microsoft Office Excel. Heres the issue:
We have a .csv file that contains 2 data columns. One for names and one for extensions. You take this file and upload it into our server that enables us to use it for other things. Heres the problem though, when uploading a new .csv file from a Mac, for some reason the program can't format correctly.
File Data Example:
Names | Extensions
Kevin | 109
James | 098
Uploaded from a PC and how program reads it:
Array ( [0] => Name [1] => Ext )
*This is correct because the first row is simply the names of what they contain
Uploaded from a Mac and how program reads it:
Array ( [0] => Name [1] => Ext Dave [2] => 209 Jose [3] => 140 )
Obviously there is a formatting issue between the two and I am not sure how to fix it.
Suggestions, ideas?
David
Note from the docs for fgetcsv()
If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
EDIT
You can enable this at runtime using
ini_set("auto_detect_line_endings", true);
I'm using the PECL extension to extract the ID3 tags from an mp3 and this is my code to print_r the array :
<?php
$tag = id3_get_tag( "/var/www/music/rem.mp3");
print_r ($tag);
?>
but when i run the file in the browser nothing happens i tried to execute it in the terminal and it works perfectly fine it gives me the output:
maniteja#maniteja:~$ sudo php /var/www/index.php
Array
(
[title] => Lexter - Who's Laughing now (Mr. Day Lens remix)
[album] => LEXTER - Who's Laughing now
[releaseTime] => 2013
[track] => 01
[genre] => (255)
[artist] => Алексей
[publisher] => Jamendo
[copyright] => 2013-11-28T21:32:03+01:00 Алексей. Licensed to the public underhttp://creativecommons.org/licenses/by/3.0/ verify at http://www.jamendo.com/album/129077/
[encodedBy] => Jamendo : http://www.jamendo.com | LAME
[commInfo] => http://www.jamendo.com
[copyrightInfo] => http://creativecommons.org/licenses/by/3.0/
[webOffAudioFile] => http://www.jamendo.com/en/track/1083749
[webOffArtist] => http://www.jamendo.com/en/artist/Mr._Day_Lens
[webOffAudioSrc] => http://www.jamendo.com/en/album/129077
[webOffPubl] => http://www.jamendo.com
[taggingTime] => 2013-11-28T20:33:57
)
is syntax wrong??? I'm new at this..
Edit:
As #ajtrichards says:
"You don't have permission to read the files in /var/www/music. You'll need to allow access to your web server"
Go to your /var/www/music folder and give the right permissions for your server to READ in there.
If you use Apache:
How to make a directory apache readable on ubuntu
PHP has two environments.
The web environment.
The CLI environment.
Your extension works perfectly on the CLI environment because you added the PECL extension on the CLI php.ini.
You need to make sure the extension is added to the web php.ini.
To test it, make a test.php with a phpinfo call.
And access it via browser.
This will output all the php.ini configuration for the "web" PHP.
Then search for your ID3 extension in all the list of extensions.
If it does not display, then is not added to the "web environment" php.ini.
To add it, you must do the same that you did to add it to the CLI php.ini
Something like, adding this line
extension=id3.so
PS:
Dude, where's my php.ini?
More info on ID3
http://www.php.net/manual/en/id3.installation.php
This works via the command line PHP as the user root has permission to read from the folder /var/www/music, as Maniteja uses sudo php to run the php file.
However, when the script is running via PHP in the web browser the web server does not have the permission to read from the folder, hence the error:
PHP Warning: id3_get_tag(/var/www/music/rem.mp3): failed to open
stream: Permission denied in /var/www/index.php on line 2
To resolve the problem - grant read access to your web server user.
I have a php web app that I deploy to multiple servers. It works on my local installation as well as every other server I put it on.
But on the current server I'm deploying to, I'm having trouble with all of the file upload features.
When trying to debug it, I realized they are failing when trying to get the finfo(FILEINFO_MIME_TYPE).
Example: a form has an upload field called "file_import". I put in the following line of code into the shared import function.
die(print_r($_FILES['file_import']));
On my local installation, it returns:
Array
(
[name] => US Capital Division.csv
[type] => application/vnd.ms-excel
[tmp_name] => C:\wamp\tmp\php3B7C.tmp
[error] => 0
[size] => 1268
)
1
Uploading the same file on the current deployment it returns:
Array
(
[name] => US Capital Division.csv
[type] => application/vnd.ms-excel
[tmp_name] => /tmp/phpJdRZDW
[error] => 0
[size] => 1268
)
1
They both say they have no errors, but the deployment server only gives the folder name for the tmp_name value, while the local installation gives the temp folder AND file name. So when my code try $_FILES['file_import']['tmp_name'] it errors.
I feel like it has to be server setting or folder setting, but I can't find anything on the web as to what would cause that.
Thanks.
Actually, the phpJdRZDW inside [tmp_name] => /tmp/phpJdRZDW is not a folder... It's just a temporary file name while the file sits inside the /tmp/ folder (waiting to be moved and [usually] renamed).
I worked with some mp3 files, and i needed to show duration of uploaded mp3 file, I used following:
http://www.zedwood.com/article/127/php-calculate-duration-of-mp3
as told in question:
PHP Function to get MP3 duration.
But now I'm facing a problem, there are some files which are not returning any file information.
the array just contains following
Array (
[Filesize] => 16756370,
[Encoding] => Unknown
)
As the "Encoding" is Unknown its not returning any data.
I found a great PHP library, getID3(), that works for VBR files as well.
You can find it right here; it's free and is being developed actively (latest version is from from February 2013):
http://sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.9.5/
I am using PHP to upload files. On the server side of the action that gets called after the upload completes is simply:
<?php
print_r($_FILES);
?>
Which outputs something like:
Array
(
[file] => Array
(
[name] => space_needlenever_lonely_alone.mp3
[type] => audio/mp3
[tmp_name] => /srv/www/uploads/php1TCIY9
[error] => 0
[size] => 3768714
)
)
The problem is I don't want to store files on the server disk ( /srv/www/uploads/php1TCIY9) at all. I actually want to store files into MongoDB using GridFS. Is there a way to stream directly into PHP without storing the file on disk on the server first? Storing files on disk requires all kinds of headaches such as permissions, and php ini configurations regarding uploads.
A websocket connection would be awesome, if I can stream the binary data directly into the server side page without writing to disk.
Or, is it possible to interact with MongoDB from a JavaScript point of view (client side) and skip interacting with PHP?
Possible, or a pipe dream?
Your PHP application is not even executed until the file upload is done. Because of this, I believe this is impossible with PHP.