Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change name of upload file in cURL?

Tags:

post

php

curl

I want to upload a file using cURL. Since cURL requires full path to the file so here is my code:

curl_setopt($ch, CURLOPT_POSTFIELDS, array("submit" => "submit", "file" => "@path/to/file.ext"));
curl_exec($ch);

However cURL will also post this full path of the file in the request header:

Content-Disposition: form-data; name="file"; filename="/path/to/file.ext"

But I want it to be just

Content-Disposition: form-data; name="file"; filename="file.ext"

So I change the code to

curl_setopt($ch, CURLOPT_POSTFIELDS, array("submit" => "submit", "file" => "@file.ext"));
chdir("path/to"); # change current working directory to where the file is placed
curl_exec($ch);
chdir("path"); # change current working directory back

And then cURL simply throws an error message

couldn't open file "file.ext"

Can anybody tell me how to do it please?

like image 513
Teiv Avatar asked Feb 28 '13 04:02

Teiv


People also ask

How do I rename a file in curl?

Either use the -o option or its alias --output , or redirect shell output to the file of choice by using > . If you want to preserve the original file name from the remote server, use the -O option or its alias --remote-name . Stores the output from the remote location in the current directory as file. html.

How do I upload a file using curl?

How to send a file using Curl? To upload a file, use the -d command-line option and begin data with the @ symbol. If you start the data with @, the rest should be the file's name from which Curl will read the data and send it to the server. Curl will use the file extension to send the correct MIME data type.

What does curl -- upload file do?

CURL upload file allows you to send data to a remote server. The command-line tool supports web forms integral to every web system. When you execute a CURL file upload [1] for any protocol (HTTP, FTP, SMTP, and others), you transfer data via URLs to and from a server.

Will curl overwrite a file?

curl attempts to cut off the directory parts from any given file name in the header to only store files in the current directory. It will overwrite a local file using the same name as the header specifies.


2 Answers

New method (since PHP 5.5) using CURLFile:

$file = new CURLFile('path/to/file.ext');
$file->setPostFilename('file.ext');

use it almost the same:

"file" => $file

Old method:

Instead of

"file" => "@path/to/file.ext"

you can tell cURL to use another filename:

"file" => "@path/to/file.ext; filename=file.ext"

That way it will use path/to/file.ext as file source, but file.ext as filename.

You'll need a very absolute path though, so you're probably missing a leading /: /path/to/file.ext. Since you're using PHP, always do a realpath():

"file" => '@' . realpath($pathToFile) . '; filename=' . basename($pathToFile);

Or something like that.

like image 116
Rudie Avatar answered Oct 07 '22 23:10

Rudie


Please correct me if I'm wrong but cURL upload won't work with relative path. It always need an absolute path, likes

$realpath = realpath($uploadfile);

So if someone wants to hide the location to his file on his webserver when uploading, either move it to a temporary folder or use fsockopen() (see this example in PHP Manual's User Contributed Notes)

like image 42
Teiv Avatar answered Oct 08 '22 00:10

Teiv