Need PHP upload form to sort files into sub directory based upon dropdown

跟風遠走 提交于 2019-12-24 19:26:57

问题


I am trying to implement a PHP script that will allow the user to select the appropriate category for their file. After selecting the category, the script should move the uploaded file to the corresponding directory for the category. An example scenario:

Categories

  • cat1
  • cat2

Subdirectories

  • cat1 = /cat1dir
  • cat2 = /cat2dir

Now the user should have a drop down giving two options (cat1 and cat2). After uploading the file, the file should automatically be moved by PHP to the appropriate sub directory. How would I go about doing this?

Although tech savvy, I have very little PHP knowledge and I can't seem to get this to work.

This is the script for the upload form

This is the form page:

<html>
<head>
<title>Submissions Script DEV</title>
<style type="text/css">
/**** Start page styles ****/

body {
    background: #DFA01B;
    font-family: arial, sans-serif;
    font-size: 14px;
    }

#wrap {
    max-width: 600px;
    margin: 30px auto;
    background: #fff;
    border: 4px solid #FFD16F;
    -moz-border-radius: 15px;
    -webkit-border-radius: 15px;
    border-radius: 15px;
    padding: 20px;
    }
</style>
<body> 
<div id="wrap">
    <div align="center">
    <a href="http://siteurl.com"><img src="logo.png" /> </a>
    <h1 style="font-family:arial">Submissions</h1>
    <p style="font-family:helvetica"> <i> Welcome to the submission page</i> </p> 
</div>
<form method="POST" action="upload.php" enctype="multipart/form-data">
    <p>
              Please enter your first name: <input type="text" name = "fname">
            <p>
              Please enter your last name: <input type="text" name= "lname">
            <p>
              Student #: <input type="text" name= "snumber"/>
            <p>
            Grade: <select name= "grade">
            <option>9</option>
            <option>10</option>
            <option>11</option>
            <option>12</option>
        </select>
        <p>
<hr>
            <!---Upload file section begins--->
              Please attach your Powerpoint (ppt/pptx/zip) file. The file should be     named student#.zip. (For example; 123456.ppt)
        </p>
        <input type="hidden" name="size" value="1024000">
            <input type="file" name="upload"> 

        <p>
        <br/>
        <br/>
        <div align="center">
        <input type=button onClick="location.href='instructions.html'" value='Back'>
        <input TYPE="submit" name="upload" title="Send your submission" value="Submit Portfolio"/>
        </div>
      </form>
<p>   
<!---Footer Styling--->
<div style="font-family: Arial;
font-size: 10px;
color: grey;
align: center;">
<!---Footer Contents--->
    <p>Copyright <a href="http://sitename.com">site</a>site</p>
</div>  
</div>
</body>
</html>

And this is upload.php (the handler for the script)

<?php

//This is the directory where images will be saved
$extension = explode(".", $_FILES['upload']['name']);
$extension = $extension[count($extension)-1];
$target = "uploads/";
$target = $target . $_POST['snumber'] . "." . $extension;


//This gets all the other information from the form
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$upload=($_FILES['upload']['name']);
$snumber=$_POST['snumber'];
$grade=$_POST['grade'];

// Connects to your Database
mysql_connect("localhost", "db_user", "dbuserpass") or die(mysql_error()) ;
mysql_select_db("sub-data") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `Submissions` VALUES ('$fname', '$lname', '$snumber', '$grade',         '$upload')") ;

//Writes the upload to the server
if(move_uploaded_file($_FILES['upload']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been recorded";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

来源:https://stackoverflow.com/questions/7509132/need-php-upload-form-to-sort-files-into-sub-directory-based-upon-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!