Remote uploading images - file_get_contents(); to help me? - php

I'm wanting to create a website that allows an user to put a link in a field and once the user submits the form, the "image" link added to the field will be remote uploaded to my server (to the path I specified in my server).
A friend of mine did this once, and he told me that he used the file_get_contents() function to get this working, but I cannot figure out how I'd do it.
Any help would be appreciated.
Thanks.

file_get_contents() will read file contents into a variable.
if you need to copy file to your server, use copy() instead.
copy($_POST['link'],basename($_POST['link']));

Related

PHP File Upload Without Form

Is there a way that I can allow a user to upload a file by providing the filename/directory for the file in a get variable (or some way other than using a form)?
Essentially, I'd like the user to be able to do something like this:
http://example.com/upload.php?file=C:/files/myFile.txt
and have upload.php handle the upload.
I've been looking at this to try to figure out how to do this, along with a couple of other resources, but they're all focused around using forms.
I need to be able to allow an app to go to my php page and provide a file to be uploaded without a prompt.
It doesn't have to be in a GET, but I CANNOT PROMPT for a file.
Any thoughts?
Sorry, that's not possible. You can upload file using post request. Why are you trying to send data using get request?

CodeIgniter upload files after preview

The flow of my multi-part form is basically Answer form -> Preview form -> Upload to database
Right now I upload my files in the Preview step since the POST data is fresh from the actual form. My problem is to access $_FILES once in the Upload to database function because var_dump($_FILES) returns array(0) {} once inside upload_to_database().
I thought of actually making hidden input[type=file] in the Preview step just so that my upload_to_database() will receive $_FILES, but this is not possible due to security reasons.
How do I solve this?
Edit
Sorry my post above was vague.
I actually want to upload once in the upload_to_database() function. I think it's bad practice to upload the user's files in the Preview stage unlike in Upload to database where they are sure of their answers.
The upload is currently in the Preview because right now it only works inside it. The $_FILES is empty once we go to upload_to_database().
How can I get a full and presistent $_FILES from Preview to upload_to_database()?
You could upload your files in the first step (when going to the preview form; like you are doing now), move them to a specific preview directory and move them to their final destination when you upload to the database.
I suppose you use something like sessions to keep track of the posted data so you could add the preview file path the same way.
Then you set up a simple cron job to delete all files older than xx hours in the preview directory every xx hours.
Edit: An alternative would be to do the preview completely client-side in javascript, like for example the preview of the questions and answers here on SO.
Isn't it possible to do something like this:
PREVIEW:
upload_to_database($_FILES['userfile']['tmp_name']);
DB:
function upload_to_database($file)
{
// work with file here
}
I mean to supply $_FILES as an argument to your upload_to_database function

Browse directory files and pass url to PHP, or upload and pass URL

I've been searching for a good 3 hours, and I'm stumped on how to do something I think is pretty simple (famous last words...)
Basically, I'm building a site that allows the user to upload a pdf file. The URL of this file is stored in a database, along with the name and a few other details. I am trying to work out how to either:
-Provide an "upload" box/button/area that a user can select a file, upload it, and then have the URL of where the file was uploaded stored in a database.
OR
-Use a separate upload script, and have the user upload the file. Then, (on a possibly separate page, I don't mind) provide a file browser, which would allow the user to browse a directory, and select a file, with the url of that file passed through to the PHP form.
I don't mind which way it is done, as long as the desired outcome of having a file uploaded and the url added to a database.
Or am I out of the reach of PHP? Is my best bet uploading files via a bare php uploader, then manually entering the url of the file uploaded into a textbox on a php form?
Any help is much appreciated!
create a htmlform, make sure to set enctype to a value of
"multipart/form-data"
in PHP you should be able to get the file namefrom the $_FILE global variable
save it to a directory on the server.
the url of your document will we http://serverroot/{directory name}/filename.pdf -> directory name is the name of the directory on the serve you saved the file in

How to get file temporary path in php while file upload

I am using Uploadify jQuery plugin for file upload.Here there is no browse button used.
it will upload the file using ajax on click event.So There is no file temp path is displaying.So i want to make it display.
So, I try to get file temp path using $_FILES['uploadfile']['tmp_name']. But it just return something like this /tmp/phpclqHXj.But i want display actual temp path like C:\Users\Diamantino\Desktop\file.jpg.Can i get this using php. any option in uploadify
plugin, anyway to get jquery or js.Any help would be appreciated.
My Browse button.
Thanks
There is no way to get the full path to the file from your user's computer. You can get the file name (in your example that would be "file.jpg") using $_FILES['uploadfile']['name']. Security issues would arise if you could get information about your user's local folder structure when they upload a file, so web browsers are very unlikely to send that data. If there is some browser that does send it, it won't appear in the $_FILES superglobal, nor will there be any other easy way to access is in php.
At the very least, you're looking for the filename found under:
$_FILES['uploadfile']['name']
Instead of C:\Users\Diamantino\Desktop\file.jpg, this will return the filename: file.jpg
However, with the use of simple Javascript, you may be able to accomplish getting the entire file path. With some logic on your end, you may be able to get the file path only when the file is uploaded. Here is an example, and some clarity on implementation.

Send image path via AJAX

so I have this form where I add item to my DB.
Fiedls are:
Name, Description, Image.
Problem is with image, I want to send the new values via AJAX using jQuery to my submit file. Reason for this is to not refresh any page. Everything is fine except IMAGE because browsers keep making the path private ex. "C:\fakepath\myImg.png".
Is it possible to somehow send my image path, because it works with default <form method="post...></form>, only problem is that it is refreshing the page, that's why I want to use AJAX.
Sorry, my native language is not English so maybe you didn't understand me clearly :)
I'm after full path to UPLOAD the image in my submit file. I can't upload image without knowing it's full path, can i?
You can use jquery.form plugin to upload forms without reloading the page.
Examples: http://www.malsup.com/jquery/form/#code-samples
The browser keeps making it a "private" path because the browser client has no knowledge of your server structure and it only knows the path where it is stored on the client machine. The upload goes to a temporary location on your server and it is up to you on the server side to move it from the temporary path to the place you want to store the file,
So in that sense, no you can't specify the destination path using AJAX.
In addition, you never want to allow the client to specify a location on your server. Even allowing them to specify the name that the file will be stored as is a potential security risk.

Categories