如何在Linux命令行上通过cURL使用PHP将文件上传到其他子目录?

时间:2018-12-26 07:52:19

标签: php curl file-upload

我正在尝试在命令行上通过curl使用PHP上传多个文件(目前,我的PHP脚本一次只能上传一个文件。)所有文件都是同一类型。但是每个文件都属于不同的学校和班级。

例如,用户在命令行上键入以下内容:

curl –F “uploadedfile=@/home/michael/test” –F “school=diamond” –F “class=alpha” http://website.com/michael/upload.php

文件名,学校和班级由用户指定。

预期结果是脚本能够将文件测试上传到alpha目录:

/michael/uploads/diamond/alpha

PHP是:

<?php
$target_path = “uploads/”;

//I tried to add these 3 lines, but it doesn't work
$school = basename( $_FILES['uploadedfile']['school']);
$class = basename( $_FILES['uploadedfile']['class']);
$target_path = $target_path . $school . "/" . $class . "/" . basename( $_FILES['uploadedfile']['name']);    

//$target_path = $target_path . basename( $_FILES[‘uploadedfile’][‘name’]);
if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
echo “The file”. basename( $_FILES[‘uploadedfile’][‘name’]. “ has been uploaded”;
} else {
echo “There was an error uploading the file, please try again!”;
}
?>

在阅读了手册之后,我发现我无法做$_FILES['uploadedfile']['school']['class']。如何修改我的PHP脚本,以便它可以捕获学校和班级并将文件上传到类子目录?

1 个答案:

答案 0 :(得分:1)

在超级全局$ _FILES数组中检索curl命令的–F“ uploadedfile = @ / home / michael / test”时,将出现另一个–F“ school = diamond” –F“ class = alpha”在$ _POST数组中。

在您的脚本中,您应该替换:

    $school = basename( $_FILES['uploadedfile']['school']);
    $class = basename( $_FILES['uploadedfile']['class']);

作者

    $school = $_POST['school'];
    $class = $_POST['class'];