Get filename from HTTP POST file upload - php

I'm working on someone else's page that has a simple form with a file upload. When I do a:
print_r($_FILES);
after the form submission I get the following:
Array
(
[input_41] => Array
(
[name] => Resources.pdf
[type] => application/pdf
[tmp_name] => /tmp/phpvVUox1
[error] => 0
[size] => 14563
)
)
I now need to get the name of the uploaded file - I'm familiar with the POST syntax of retrieving form field input values such as:
$_POST['input_32']
but not sure how to get the file name within an array.

As you can see in your print_r() output, you already have name property of the file in it, You can use this to get the name of the file.
$_FILES["input_41"]["name"]
Reference: http://php.net/manual/en/reserved.variables.files.php

Related

File type missing in file upload

I have a page that will allow me to upload a CSV file. Using PHP I then take the contents of the CSV file and insert them into a MySQL database. I've been doing this with no problems for awhile now. Then today, I tried uploading the file and it didn't work. The problem is that there appears to be no file type associated with the CSV file. For example, on a good upload, the debug messages I spit out look like this:
You uploaded a file: buylist_235.csv
file type: application/vnd.ms-excel
file size: 10456
File is valid, and was successfully uploaded. Here is some more debugging info:Array (
[csv] => Array
(
[name] => buylist_235.csv
[type] => application/vnd.ms-excel
[tmp_name] => C:\xampp\tmp\php3C1E.tmp
[error] => 0
[size] => 10456
)
)
However, when I upload the problem file with the debug messages on I noticed the file type and file size is empty.
You uploaded a file: buylist_235_1.csv
file type:
file size: 0
Possible file upload attack!
Here is some more debugging info:Array
(
[csv] => Array
(
[name] => buylist_235_1.csv
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)
)
Anyone have any ideas why the file type is empty? In Windows Explorer, if I hover over either of these files the info balloon over the files say "Type: Microsoft Excel Comma Separated Values File". But there is something wrong with the second file that causes my PHP to puke and not recognize the file type.
The file type is not the problem (I don't think).
You'll see error code 2 was returned during the attempted file upload in your second example block, and it was NOT written to temporary storage (tmp_name is empty, size is 0).
You uploaded a file: buylist_235_1.csv
file type:
file size: 0
Possible file upload attack!
Here is some more debugging info:Array
(
[csv] => Array
(
[name] => buylist_235_1.csv
[type] =>
[tmp_name] =>
[error] => 2
[size] => 0
)
)
Error code 2 is: UPLOAD_ERR_FORM_SIZE: The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
Adjust this value larger in your form HTML.

How to get full path of image in php

I want to get the full path of image in PHP. I used <input> tag for image uploading. I'm unable to get the full path. when I alert the value of <input type="file">, I'm getting
c:/fake-path/image.jpg
Here is my code:
<input type="file" name="upload_captcha_background" id="upload_captcha_background" />
var file_path= jQuery("#upload_captcha_background").val();
alert(file_path);
and in PHP I'm fetching the value like:
$ux_image_path =$_FILES['upload_captcha_background'];
If you mean the path where the image was located on the user computer before he/she uploaded it to your form - you can never know it in php or javascript or anywhere else.
In PHP you can see the path on SERVER (usually in the temporary folder) where the file was stored so you can read or copy it. If may just happen that the server is as well your user computer (i.e. you are testing the website on http :// localhost) but don't confuse those two things.
You can never be sure of getting a full filepath or even a reliable filename or content-type submitted in a file upload file.
you should never do so....and i think trying it in latest browsers is useless(from what i know) .. all latest browsers on the other hand , will not allow this.
Some related Stackoverflow questions:
How to get the full path of the file from a file input
Full path from file input using jQuery
Retrieving the full path (server-side) of a file uploaded using Firefox
the structure for
$_FILES[]
as follows
/*
Array
(
[image] => Array
(
[name] => Array
(
[0] => 400.png
)
[type] => Array
(
[0] => image/png
)
[tmp_name] => Array
(
[0] => /tmp/php5Wx0aJ
)
[error] => Array
(
[0] => 0
)
[size] => Array
(
[0] => 15726
)
)
)
*/
if you are trying to upload a file and copy it to a directory
use the following code
$tmp_name = $_FILES['upload_captcha_background']["tmp_name"];
$name = $_FILES['upload_captcha_background']['name'];
move_uploaded_file($tmp_name, "$uploads_dir/$name");

php file upload type

I'm trying to upload a zip file and a csv file from HTML form.
On PHP, When I printed $_FILES (Actually $request->getFiles() in symfony), I got following.
Array
(
[zipfile] => Array
(
[name] => tempfiles.zip
[type] => application/octet-stream
[tmp_name] => C:\wamp\tmp\php5D42.tmp
[error] => 0
[size] => 850953
)
[csvfile] => Array
(
[name] => test.csv
[type] => application/vnd.ms-excel
[tmp_name] => C:\wamp\tmp\php5D52.tmp
[error] => 0
[size] => 312
)
)
I'm wondering with the type and tmp_name. I need to take few decisions based on type. Is it safe to take decisions on existing type? Will I get same result for similar files on Linux server?
Again tmp_name have .tmp extension. Is it consistent on both windows/linux? If not, is there any way that the code I write on windows (decision using type) will work on linux without any issue?
Using this type can be dangerous Because user can change the type of the files and can upload a php script.
You should validate the type first just like get_image_size() to validate a image file.I have no idea about .zip file
It is not safe to trust the type form $_FILES, you need to validate the file type in server side.
For .tmp extension, it is ok both on windows or linux.

problem in uploading FLV video file to server

while uploading MP4 video $_FILES array comes like this..
Array
(
[qqfile] => Array
(
[name] => video.mp4
[type] => video/mpeg4
[tmp_name] => /tmp/php74N9mR
[error] => 0
[size] => 199160
)
)
But while uploading .FLV file $_FILES array coming like this , why it is not coming proper?
Array
(
[qqfile] => Array
(
[name] => YouTube - My Youtube Contest Announcement.flv
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
please suggest.
The file you are trying to upload is too large. From the PHP manual's chapter on file uploads:
Since PHP 4.2.0, PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in $_FILES['userfile']['error'].
...
UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
i think its related to the file name you upload ,
when you call to the php function wrap your file name with ''

uploaded file not returning a valid file type attribute?

I can't understand why my file uploaded through a form that used to be working just fine, is now returning an empty image type. Below is the printed array of $_FILES:
[file] => Array ( [media_image] => Array ( [name] => 001_ac.jpg [type] => [tmp_name] => [error] => 1 [size] => 0 )
According to the File error #1, the file you are uploading is larger than the upload_max_filesize allowed in php.ini. See the error codes in the manual here: http://php.net/manual/en/features.file-upload.errors.php
Hence, the file upload is being rejected, so it hasn't got a size or most of its other normal attributes. Your code can check for such errors and handle them by giving the user feedback, such as informing them of file size restrictions, file types, etc.

Categories