Home > Backend Development > PHP Tutorial > post - 如何用curl命令上传文件到制定的目录?后台用php程序来接收,apache httpd做服务器

post - 如何用curl命令上传文件到制定的目录?后台用php程序来接收,apache httpd做服务器

WBOY
Release: 2016-06-06 20:13:25
Original
1601 people have browsed it

用curl命令上传文件到服务器,但是只能传到/tmp文件夹中,请问这是为什么呢?
linux命令行输入:

<code>curl -F upload=@/root/yifei.txt http://server_ip/upload.php</code>
Copy after login
Copy after login

在server端的/var/www/html/中放入文件upload.php

<code><?php print_r($_FILES);
if(isset($_FILES['upload'])){
  @copy( $_FILES['upload']['tmp_name'], '/tmp/'.$_FILES['upload']['name'] );
  @unlink( $_FILES['upload']['tmp_name'] );    
}
?></code>
Copy after login
Copy after login

在这里只能制定tmp目录,如果制定其他目录会上传失败,所以只能传到/tmp文件夹里面,请问如何才能上传到指定目录呢?

参考:http://www.aslibra.com/blog/post/curl_po...

回复内容:

用curl命令上传文件到服务器,但是只能传到/tmp文件夹中,请问这是为什么呢?
linux命令行输入:

<code>curl -F upload=@/root/yifei.txt http://server_ip/upload.php</code>
Copy after login
Copy after login

在server端的/var/www/html/中放入文件upload.php

<code><?php print_r($_FILES);
if(isset($_FILES['upload'])){
  @copy( $_FILES['upload']['tmp_name'], '/tmp/'.$_FILES['upload']['name'] );
  @unlink( $_FILES['upload']['tmp_name'] );    
}
?></code>
Copy after login
Copy after login

在这里只能制定tmp目录,如果制定其他目录会上传失败,所以只能传到/tmp文件夹里面,请问如何才能上传到指定目录呢?

参考:http://www.aslibra.com/blog/post/curl_po...

把临时目录下的文件移动到目标上传目录

move_uploaded_file($file['tmp_name'], $filename)

参考http://php.net/manual/zh/function.move-u...

move_uploaded_file(),下面是我用yii框架写的:

<code>if (!empty($_FILES['avatar']['name'])) {
    $file_name = $_FILES['avatar']['name'];
    $path = Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . $file_name; //图片保存路径
    $file = $_FILES["avatar"]["tmp_name"];
    move_uploaded_file($file, $path);
}</code>
Copy after login

Yii::getAlias('@webroot')对应物理路径'D:/xampp/htdocs/project/web',linux服务器的话会有不同。

可以选择copy一份过去,注意文件夹的权限问题;
move_uploaded_file()也是可以,但还是得注意文件夹是否有写入权限

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template