0

I am trying to upload multiple files using PHP via curl on the command line (For now, my PHP script is only able to upload one file at a time.) All files are of the same type. But each file belongs to different school and class.

For example, the user types the following on the command line:

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

File name, school and class are specified by the user.

The expected outcome is the script is able to upload the file test to the alpha directory:

/michael/uploads/diamond/alpha

The PHP is:

<?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!”;
}
?>

After reading the manual, I noticed that I can't do $_FILES['uploadedfile']['school']and ['class'].How could I modify my PHP script so that it can capture the school and class and upload the file to the class subdirectory?

1 Answer 1

1

While the –F "uploadedfile=@/home/michael/test" of the curl command will be retrieved in the superglobal $_FILES array, the other –F "school=diamond" –F "class=alpha" will appear in the $_POST array.

In your script, you should replace :

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

by

    $school = $_POST['school'];
    $class = $_POST['class'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.