How to get file source path from users PC when they submit their file on form like below.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" name="submit" value="Submit">
</form>
Example path that I want is file:///C:/Users/hafizul/Downloads/myimage.png
So, I can display user image using the code below:
<?php
$path = 'file:///C:/Users/hafizul/Downloads/myimage.png';
header('Content-Type:image/png');
echo file_get_contents($path,FILE_USE_INCLUDE_PATH);
?>
Thanks!
Since PHP is a server-based language, you cannot pass him parameters of a Path on the client computer: the code is executed on the server without access or knowledge of the client and its hard drive.
A better option in this case is to use JavaScript or Flash on your page to display the image, the user can review it before submitting it and actually upload it on the server.
Check here for instance for the JavaScript version:
http://teck.in/javascript-to-preview-local-image.html
Note that your code might be blocked by security measures on the site since the access of a script to the local computer filesystem might be harmful.
You will not know where the file came from on their computer reliably, but you can send an uploaded file back to the browser as you seemed to be attempting:
header("Content-type: image/png");
readfile($_FILES['file']['tmp_name']);
Refer to the PHP manual chapter "Handling file uploads":
http://php.net/manual/en/features.file-upload.php
Related
I'm trying to write code in php so that when the .php file is opened, it automatically uploads from a specific file address on my windows computer to the localhost server.
This is my attempt but I'm not sure I fully understand how to do this without using an HTML form where the user specifies the file they want to upload.
<?php
$target = 'UPLOADED_FILE.csv';
move_uploaded_file('C:\Users\Ken.Feier\Desktop\temp\REPORT.csv', $target);
?>
I want the code to take the REPORT.csv file from my personal computer and upload it to our server with the file name UPLOADED_FILE.csv
UPDATE: I see that my problem will not be solved with php. Can anyone recommend any other solution involving Filezilla or any other FTP that can be automated?
That's not how it should be done.
You need a page with a html form, which will send the data to server on submit. Note that the file could be stored on your personal.
Form code e.g.
<form method="post" action="destination.php" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" value="upload" />
</form>
Then, on the server, you can use the $_FILES['filename'] which contains your file's infos. Note that when a file is uploaded to the server, it's stored in the tmp folder, which is temporary, so you have to move your file to a persistent directory with move_uploaded_file(); (move_uploaded_file Docs)
E.g:
<?php
$file = $_FILES['filename'];
move_uploaded_file($file['tmp_name'], '/new/destination/for/the/filename.php');
I am able to upload other file types like .txt, .png, .apk using
<form action="index1.php" method="post" name = "mySuperForm"
enctype="multipart/form-data">
<input type="text" placeholder="Application Name" name="appname">
<input type="text" placeholder="Version Number" name="appversion">
Application File: <input style = "width:auto" type="file" name="file" id="file"><br>
</form>
But, when I try to upload a .ipa file, I can't grab the application name or version number on index1.php (the page I am posting to)using $_POST. However, if I upload a different file type, I can. It's as if nothing is getting posted if I try to upload an ipa file, like the html is failing on that line. I am using my localhost, wamp server. Any advice?
Without seeing your handling PHP, it's a bit difficult to diagnose, but here are a few potential items that may point you in the right direction:
Does the .IPA file exceed the *post_max_size* or *upload_max_filesize* as defined in your PHP configuration?
Try var_dump($_FILES); to see if your script is seeing the files there.
I think this is happening because the file is too big and the request is being truncated by the web server, so PHP would not be able to access any form fields because the request was not completed. How large is the ipa file? You might need to adjust your maximum post size. The default is usually 4 MB.
I'm using wampserver on my computer and wrote a simple html-form:
<form name="test" action="upload_file.php" method="post">
<label for="file">Filename:</label>
<input type="file" name="picurl" id="file" ><br>
<input type="submit" name="submit" value="Submit" >
</form>
When I click on "browse" and open a file ,for example pil.png it shows in the input textarea the full path(C:\Users\hope\Desktop\images\pil.png)
I want this exact link saved but when I try to catch it $name = $_POST["name"];it only displays this- "pil.png" not the full path. why?
You cannot get the complete local file path. Only the file data itself and its name is submitted to the server. The file path being displayed in the input element is only visual styling, it has no functionality.
File elements are very limited for security reasons, and that's a good thing.
The browser doesn't submit the full path because it would be a privacy problem, you would be exposing your file system structure to the server. It's not relevant to the server and the server doesn't need to know where the file was located on the client's filesystem.
You are getting only file_name because you are not asking for path.
to get full path you have to use
public string SplFileInfo::getRealPath ( void )
try
var_dump($_FILES['picurl']);
I want to upload the files to this address: http://chusmix.com/Imagenes/grupos and I'm trying with this simple this code but it doesn't work:
<form enctype="multipart/form-data" method="post" action="http://chusmix.com/Imagenes/grupos">
Please specify a file:<br>
<input type="file" name="datafile" size="40">
</p>
<div>
<input type="submit" value="Send">
</div>
</form>
Oddly enough, the first result of a Google search yielded this rather helpful tutorial. Why not read it?
Read the PHP manual chapter "Handling file uploads":
http://php.net/manual/en/features.file-upload.php
The way you think uploads work is not the way they work. The form posts to the script you want to handle the request, not the location you want the uploads to be. When you upload a file to Apache, it places that file in the temporary directory of the computer (in Linux, that's /tmp by default).
Your script has to move the file from the temp directory to wherever you want it to be. The manual has plenty of code showing you how.
Make sure the form is loaded via
http://chusmix.com/Imagenes
The browsers wont you allow to upload to a unkown website (Same origin policy).
Edit your form
<form enctype="multipart/form-data" method="post" action="/grupos">
I have an image uploader that uses the imgur.com API and jQuery's .ajax() function to upload images to their servers. However, if I browse for an image using the <input type="file"/> element of the form, it will only be successful in uploading an image if the image file is found in the same directory as the page.php file that the form is found in (shown below). How can I allow the form to upload images from any directory on my computer?
page.php:
<form action="page.php" method="post">
<input type="file" name="doc" id="doc" /><br/>
<input type="image" src="go.gif" name="submit" id="submit" />
</form>
You've forgotten the enctype="multipart/form-data" attribute on your form tag, for one. Without that, file uploads generally don't work too well.
Beyond that, the server won't really care what directory you're uploading FROM, especially under PHP. The uploaded copy on the server is stored with temporary filename ($_FILES['file']['tmp_name']) that has absolutely nothing to do with the directory/filename on your computer.
Once it's on the server, you'll have to actually move that temporary file somewhere else, as PHP will auto-delete it once the script terminates and you've not handled it yourself. move_uploaded_file() is what's generally used to take of that process.
Perhaps this is the only folder with write-permissions.
I guess it is jquery that is doing the actual posting to http://imgur.com/api/upload as the form is just posting to itself, so my guess is that jquery / javascript can only read files in your web-space and not on your whole hard-drive.