-1

我在为多个文件上传实现此脚本时遇到问题。用户需要准确指定将文件放入的文件夹(课程代码),因此 $_POST 函数

这是我的 HTML

<form enctype="multipart/form-data" action="uploader.php" method="POST">
   Choose Course Code:
   <select name="subject_list">
        <option value="ACC 100">ACC 100</option>
        <option value="ACC 406">ACC 406</option>
        <option value="ECN 104">ECN 104</option>
        <option value="ECN 204">ECN 204</option>
        <option value="FIN 300">FIN 300</option>
        <option value="GMS 200">GMS 200</option>
        <option value="ITM 100">ITM 100</option>
        <option value="ITM 301">ITM 301</option>
        <option value="ITM 305">ITM 305</option>
        <option value="ITM 330">ITM 330</option>
        <option value="ITM 350">ITM 350</option>
        <option value="ITM 407">ITM 407</option>
        <option value="ITM 500">ITM 500</option>
        <option value="ITM 501">ITM 501</option>
        <option value="ITM 505">ITM 505</option>
        <option value="ITM 600">ITM 600</option>
        <option value="LAW 122">LAW 122</option>
        <option value="SSH 105">SSH 105</option>
        <option value="Other">Other</option>
   </select>
    <br>
   Choose a file to upload (Max 500MB): <input name="rye_file[]" type="file" id="file_style" multiple />
    <input type="submit" name="submit" value="Upload" />
</form> 

<form action="/logout.php">
   <input type="submit" value="Logout">
</form>

这是我的PHP:

<?php
   session_start();

    // IF USER NOT REMEMBERED(DID NOT CLICK REMEBER ME BUTTON) OR NO SESSION IS FOUND THEN THROW HIM OUT TO LOGIN
   //SECURITY SO THAT USERS CANT ACCESS WEB URL DIRECTLY
   if (!isset($_SESSION['myusername']) && !isset($_COOKIE['myusername'])) {
        header("Location: index.php");
    }

   // SUBJECT_LIST IS FOR USER TO CHOOSE FOLDER
   $subject_list = $_POST['subject_list'];
   $uploaddir = "/var/www/fixnode_website/content/Secure Login/Rye High/uploads/$subject_list";
   $files=array();
   $fdata=$_FILES['rye_file'];
   if(is_array($fdata['name'])){
        for($i=0;$i<count($fdata['name']);++$i){
            $files[]= array(
                            'name'     => $fdata['name'][$i],
                            'tmp_name' => $fdata['tmp_name'][$i],
                            );
        }
   } else $files[]=$fdata;

   foreach ($files as $file) {
      // uploaded location of file is $file['tmp_name']
      // original filename of file is $file['name']
        if (move_uploaded_file($file['tmp_name'], "$uploaddir/$file['name']")) {
            echo "File is valid, and was successfully uploaded.\n Please wait, your browser will refresh in 5-10 seconds!";
        } else {
            echo "Upload failed";
        }
   }
?> 

我不断收到错误 500 服务器错误。如果有人可以帮助修改我的代码以匹配我的参数,那么我将不胜感激

谢谢,菲利普·库贾瓦

4

4 回答 4

1

问题是权限问题,我上传到的文件夹没有适当的权限。我使用 chmod -R 777 更改权限,以便 php 可以上传到该文件夹

于 2013-02-27T17:48:36.883 回答
0

这是使用 PHP 和 HTML 进行多文件上传的示例

https://github.com/hemantrai88/html5-php_multi-file-upload

于 2013-12-23T08:27:49.473 回答
0

尝试使用此代码进行多次上传:-

<!DOCTYPE html>
<html lang="en-us">


    <!-- <title>HTML5 Multiple File Upload</title>----->
    <!-- <link rel="stylesheet" href="s.css" media="screen">--->
    <!-- <style media="screen"></style>--->
    <form action="stack.php" method="post" enctype="multipart/form-data">
        <input type="file" value="" name="upload_file[]" multiple>
        <button type="submit">Upload!</button>
    </form>
<?php
for($i=0;$_FILES["upload_file"]["name"][$i]==true;$i++)
{
$fileName = $_FILES["upload_file"]["name"][$i]; // The file name
$fileTmpLoc = $_FILES["upload_file"]["tmp_name"][$i]; // File in the PHP tmp folder
$fileType = @$_FILES["upload_file"]["image/png||image/jpg"][$i];  // The type of file it is
$fileSize = $_FILES["upload_file"]["size"][$i]; // File size in bytes
$fileErrorMsg = $_FILES["upload_file"]["error"][$i]; // 0 = false | 1 = true
$kaboom = explode(".",$_FILES["upload_file"]["name"][$i]); // Split file name into an array using the dot
$fileExt = end($kaboom); // Now target the last array element to get the file extension

$moveResult= move_uploaded_file($fileTmpLoc, "foldername/$fileName");
unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder

}

?>
于 2013-02-27T09:28:52.250 回答
0

这就是我处理 HTML5 多次上传的方式:

if (!empty($_FILES["rye_file"]["name"])) {
foreach ($_FILES["rye_file"]["error"] as $key => $error) {
    //something went wrong...
    if ($error != UPLOAD_ERR_OK) {
        $aErrors[] = 'upload failed_ with error code: '.$error;
        continue;
    }
    if (!move_uploaded_file($_FILES["rye_file"]["tmp_name"][$key], 'uplaod_folder/'.$_FILES["rye_file"]["name"][$key])) {
        $aErrors[] = 'move_uploaded_file failed';
    }
}
}
于 2013-02-27T09:30:20.727 回答