简体   繁体   中英

Send file with cURL

After several hours of research, I could not solve a problem with PHP and cURL.

When I try to send a file directly from the form, the curl work normally.

<form method="post" action="" enctype="multipart/form-data">
<input name="file" type="file" /> <br />
<input name="submit" type="submit" value="Upload" />
</form>

<?php
$temp = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];

$post = array (
    'file' => '@'. $temp
);

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
$exec = curl_exec ($ch);
curl_close ($ch);
?>

The above code is working properly. When I try to use your form, the file is sent correctly.

My problem is that I need to send files that are already on the server.

I tried with the full path to the file "C:/xampp/htdocs/test/photos.zip" but for some reason, does not work.

$post = array (
    'file' => '@C:/xampp/htdocs/test/photos.zip'
);

Does anyone know how I do to send files that have already been sent to the server?

Edit:

upload.php ( server )

<?php
error_reporting( E_ALL );
$upload = $_FILES['file'];
move_uploaded_file( $upload['tmp_name'], 'photos.zip');
?>

myuploadtest.php ( localhost )

<form action="" method="post" enctype="multipart/form-data">
<input name="file" type="file" /><br />
<input name="submit" type="submit" value="Upload" />
</form>

<?php
$temp = $_FILES['file']['tmp_name'];
$name = $_FILES['file']['name'];

$post = array(
    'file' => '@'.$temp
);

$url = "http://www.mysite.com/upload.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$exec = curl_exec($ch);
curl_close($ch);
?>

uploadcurl.php ( localhost )

<?php
$post = array(
    'file' => '@C:/xampp/htdocs/test/photos.zip'
);

$url = "http://www.mysite.com/upload.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$exec = curl_exec($ch);
curl_close($ch);
?>

Thanks in advance.

Here is something you might like to try :

  • Use relative paths, ie, give the path name relative to the php script that you are running.

  • In the code above, I think you are missing the declaration of $url.

  • I don't quite understand the question Does anyone know how I do to send files that have already been sent to the server? Which server are you sending the file to? (localhost?)

If the relative path doesn't work, could you share the error messages that you are getting?

To send a file that's already on the server, locate it with its full path on the server:

$post = array(
    'file' => '@'. $_FILES['file']['tmp_name']
);

Which is exactly what you did in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM