How to get full path of image in php - 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");

Related

Get filename from HTTP POST file upload

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

Create Image from numerical array of data in php

Am dealing with webservices in my project. i am not sure about the bankend logic like what kind of method they used to store images in database.
This is the response i got as my profile image from server
Array
(
[0] => -119
[1] => 80
[2] => 78
[3] => 71
[4] => 13
[5] => 10
[6] => 26
.
.
.
[3778] => 123
[3779] => 60
[3780] => -82
[3781] => -57
[3782] =
)
How do convert this numerical array into PNG Image?
I have created a new image file and put all contents into that file using
file_put_contents('downloads/myImage.png', $profileImage);
$profileImage variable contains the above array.
this code creates a new image with some dump of datas. I know i have missed something by doing this. please help me with this.
i have used this same code to create PDF files from the server in this same project. that's working fine. thats why i tried with the same code to get my image. But its not working with images
Your question is a little bit unclear — I can't tell if you want to save the PNG file somewhere or deliver it as the response to an HTTP request. Also, you seem to imply that $profileImage is an array variable, but provide it as an argument to file_put_contents(), which expects a string.
But either way, the binary data you are being provided with looks fine.
If $profileImage is an array of signed byte values, then you can convert it to a string of binary data with the following command"
$rawPNG = implode(array_map('chr',$profileImage));
If you need to, you can then use file_put_contents() to write this data to a file. (The function is binary-safe, so there shouldn't be any problems.)
Alternatively, if you want to return a PNG image to a web client, just echo it with an appropriate Content-Type header:
header('Content-Type: image/png');
die ($rawPNG);

PHP File upload wrong type

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.

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.

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