PHP file upload and curl
I have a question.
I have a script that handles file upload, and after the file is done uploading, I send $_FILES data over to another local script via curl, which handles the files and puts开发者_运维问答 it into proper place.
The problem is, it works perfectly on my local, using following curl settings:
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
I run windows 7, but when I moved the script to my dedicated server (CentOS), it stopped working.
After doing some research, when the file is uploaded, it is stored in /tmp directory.
It turns out, the file uploaded to /tmp is deleted right before my curl call. It is known that PHP deletes tmp file uploads once the script finishes executing.
Is there a setting I could use in CURL to bypass this problem? It works fine locally, I just don't understand why it wouldn't work on my CentOS server..
UPDATE: It worked on my other server, which runs on linux as well... I don't know what particular setting it is to change this, but it seems like every server configuration is different on this.
Because you are using CURL you are sending the web server a new request. There are a couple of things you are asking the web server to do:
- get uploaded file
- process script send
- new request to web server process
- second request
in the mean time, the engine knows it needs to perform the following task; tidy up uploaded files
The different environments opens the possibilty to process those tasks in a different order (e.g. Linux+Apache vs Windows+IIS). When it decides to tidy up the uploaded files would explain what you are seeing:
- get uploaded file
- process script send
- new request to web server
- <- here
- process second request
- <- here
in the first position, your script breaks, in the second it will work. This is because in the first spot your uploaded file has been deleted before your second request/script is processed/run. Like Marc mentioned, this is core functionality so you will need to modify your script to use move_uploaded_file() and then pass the location of the file to your other script.
Hopefully that sheds some light on why it's operating differently in the different environments.
You'd have to move the file out of /tmp to another directory using move_uploaded_file()
when the original upload finishes, or initiate the curl upload from within the same script. Otherwise PHP will clean up the file and there's not much else you can do.
The automatic cleanup is core PHP functionality, and curl can't affect it.
精彩评论