Ini adalah script php yang berperan untuk mengirim file.Kita simpan ke folder htdocs dengan nama file uploadfile.php di dalam folder /testupload/ :
<?php
$file = 'gambar.jpg'; //file yang mau di upload gambar.jpg
if(file_exists($file)){
$filelocate = dirname(__FILE__).'/'.$file; //absolut path file gambar.jpg
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1/testupload/upload/terimafile.php');
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); //disable verify ssl
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); //disable verify ssl
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, array('filemasuk' => curl_file_create($filelocate)));
$responseContent = curl_exec($curl);
curl_close($curl);
echo $responseContent; //response upload
}
else{
echo 'file '.$file.' tidak ada';
}
?>
Setelah menyimpan uploadfile.php, jangan lupa membuat folder /upload/ dan melampirkan file gambar dengan nama gambar.jpg.
![]() |
| uploadfile.php dan gambar.jpg di dalam folder /testupload/ |
<?php
if(!empty($_FILES['filemasuk'])){
$simpan = @copy($_FILES["filemasuk"]["tmp_name"], $_FILES["filemasuk"]["name"]);
if($simpan){
echo 'upload berhasil'; //upload berhasil diupload
}
else{
echo 'upload gagal'; //file gagal diupload
}
}
else{
echo 'file tidak di input';
}
?>
![]() |
| terimafile.php di dalam folder /testupload/upload/ |
sekarang coba kita buka http://localhost/testupload/uploadfile.php.
![]() |
| upload berhasil dengan php |
coba kita cek folder apakah file gambar.jpg benar-benar sudah ter-upload
![]() |
| file gambar.jpg yang sudah diupload |
sekian pembahasan php curl upload.




No comments:
Post a Comment
Diharapkan berkomentar dengan sopan dan santun, terimakasih.