How to upload image file to remote server with PHP cURL
In this example we learn how to upload static files to remote server using PHP cURL library.
We must know following functionality before we continue this example
- PHP file upload functionality.
- cURL function reference.
- how to submit data to remote server with PHP cURL
Files and Folders
For this example we use three files
-
uploader.php
-that recieves the files on to the remote server.
-
handler.php
-is used to web based client to upload the file.
-
curl_handler.php
-PHP cURL uploading client to upload file.
Since we use same host to both local and remote server ,we create seperate directories
-
local_files/
- these files will be uploaded to the remote server ,means to uploaded_files
directory.
-
uploaded_files/
- these files are from the uploding client from both web based
and PHP cURL based
.
uploader.php
$upload_directory=dirname(__FILE__).'/uploaded_files/';
//check if form submitted
if (isset($_POST['upload'])) {
if (!empty($_FILES['my_file'])) {
//check for image submitted
if ($_FILES['my_file']['error'] > 0) {
// check for error re file
echo "Error: " . $_FILES["my_file"]["error"] ;
} else {
//move temp file to our server
move_uploaded_file($_FILES['my_file']['tmp_name'],
$upload_directory . $_FILES['my_file']['name']);
echo 'Uploaded File.';
}
} else {
die('File not uploaded.');
// exit script
}
}
handler.php
<form method="post" enctype="multipart/form-data" action="uploader.php" id="form1">
<input type="file" name="my_file" id="my_file"><input type="submit" value="Upload" name="upload" id="upload">
</form>
curl_handler.php
$local_directory=dirname(__FILE__).'/local_files/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/curl_image/uploader.php' );
//most importent curl assues @filed as file field
$post_array = array(
"my_file"=>"@".$local_directory.'shreya.jpg',
"upload"=>"Upload"
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
$response = curl_exec($ch);
echo $response;
From 网上下载
分享到:
相关推荐
php大文件上传,百兆文件上传,有进度条,有上传速度 之前发布一个大文件上传,但是问题太多,此版本相对上一个稳定得多 1,将压缩包里的 php_uploadprogress.dll 文件复制到你的PHP 安装目录里的扩展库文件夹 ...
PHP文件上传类,自定义配置,包括单文件和多文件上传(检测文件大小,文件格式,文件重命名,错误返回,全部封装在一个类中)
php大文件上传案例,支持断点续传。
本文实例讲述了PHP实现通过CURL上传文件功能。分享给大家供大家参考,具体如下: PHP使用CURL上传文件只需发送一个POST请求就可以了,在请求中设置某个字段为需要上传的...为了把这个文件上传给服务端的脚本http://you
网上找来的,很好用的PHP上传文件源码,亲测。
php文件上传插件(简洁好用的上传插件)需要的赶快来下载
强大的php文件上传类强大的php文强大的php文件上传类件上传类
php文件上传函数,直接用就好了 觉得好用,就分享了
php大文件上传例子,包括实现的步骤及详细说明
php文件上传代码
上传文件php代码,选择本机上的文件,php上传文件是转到缓存中,本代码实现了真正的上传,不是缓存文件
通过php实现文件上传,可以将文件上传至服务器端
使用wamp集成工具,结合phpStrom写的PHP文件上传图片,上传后并在HTML页面上展示的例子
PHP文件上传类源码PHP文件上传类源码
前端Javascript+Html5+后端PHP分块上传文件,PHP分块上传大文件,该项目可以正常运行,入口为index.html,需要正确配置fileDir的读写权限 目前有测试过上传1.5G左右的没有问题(未测试更大的文件),理论上更大的...
php文件上传进度条
php文件上传类,能自定义上传文件类型,设置文件上传大小,能满足你所需求的
最近因工作需要开发一个视频网站涉及到百兆视频上传,于是查阅各种资料实现php大文件上传,拿出来和大家共享下。 实现方法php_apc扩展。在php.ini自行安装apc扩展并作相应设置。
UploadFile.class.php支持多文件上传的上传类 说明:支持多文件上传的上传类 使用方法: include '../UploadFile.class.php'; $upload=new UploadFile(); if(!$upload->upload()) { // 上传错误提示错误信息 $...