首页 > 解决方案 > PHP/Mysql:尝试为以前创建的子文件夹设置上传目录路径,其 id 如下:(upload/echo matriculation)

问题描述

我正在尝试为以前创建的子文件夹设置上传目录路径,其 id 如下:(upload/echo matriculation),我在这里做错了什么?

非常感谢您的帮助。

我正在尝试获取保存在表中作为“学生”的学生的预科,并在 upload/ 后插入它,在以前的菜单中,我正在创建一个文件夹,其中学生的预科作为他的上传文件夹。

例如:

学生1:预科6500000

上传路径应为:upload/6500000

if(isset($_REQUEST['update_id']))
{
    try
    {
        $id = $_REQUEST['updat`enter code here`e_id']; //get "update_id" from stproj_folder.php page through anchor tag operation and store in "$id" variable
        $select_stmt = $pdo->prepare('SELECT * FROM studentprojects WHERE id =:id'); //sql select query
        $select_stmt->bindParam(':id',$id);
        $select_stmt->execute(); 
        $row = $select_stmt->fetch(PDO::FETCH_ASSOC);
        extract($row);
    }
    catch(PDOException $e)
    {
        $e->getMessage();
    }
    
}


if(isset($_REQUEST['btn_update']))
{

    $studid = $_POST['studid'];
{
    try
    {

        
        $file_path1_file    = $_FILES["file_one"]["name"];
        $type       = $_FILES["file_one"]["type"];  //file name "file_one"
        $size       = $_FILES["file_one"]["size"];
        $temp       = $_FILES["file_one"]["tmp_name"];
            
        $path="upload/".$file_path1_file; //set upload folder path
        
        $directory="upload/".$studid; //set upload folder path for update time previous file remove and new file upload for next use
        
        if($file_path1_file)
        {
            if($type=="image/jpg" || $type=='image/jpeg' || $type=='image/png' || $type=='image/gif'|| $type=='application/pdf') //check file extension
            {   
                if(!file_exists($path)) //check file not exist in your upload folder path
                {
                    if($size < 50000000) //check file size 5MB
                    {
                        unlink($directory.$row['file_path1']); //unlink function remove previous file
                        move_uploaded_file($temp, "upload/" .$file_path1_file); //move upload file temporary directory to your upload folder    
                    }
                    else
                    {
                        $errorMsg="Ihre Datei ist zu groß. Maximal 50 MB erlaubt. Nutzen Sie hierzu das PDF24 Tool, um die datei zu verkleinern."; //error message file size not large than 5MB
                    }
                }
                else
                {   
                    $errorMsg="Diese Datei wurde bereits in einem anderen Datenbank Eintrag hochgeladen...Bitte Upload Ordner und/oder Datenbank nach entsprechender studentischen Arbeit überprüfen."; //error message file not exists your upload folder path
                }
            }
            else
            {
                $errorMsg="Zum Upload nur PDF, JPG, JPEG, PNG & GIF File Formate freigegeben.....Bitte Dateiendung überprüfen!"; //error message file extension
            }
        }
        else
        {
            $file_path1_file=$row['file_path1']; //if you not select new pdf than the previous pdf sam it is it.
        }
    
        if(!isset($errorMsg))
        {
            
            $update_stmt=$pdo->prepare('UPDATE studentprojects SET file_path1=:file_up WHERE id=:id'); //sql update query
            $update_stmt->bindParam(':file_up',$file_path1_file);   //bind all parameter
            $update_stmt->bindParam(':id',$id);
             
            if($update_stmt->execute())
            {
                $updateMsg="Datei erfolgreich hochgeladen...........";  //file update success message
                Header("Refresh:0 ");
            }

        }
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    
}
}


?>


      <!------LOGO HEADER-->
      <?php include ('inc/header.php');?>
<?=template_header('Read')?>



<div class="content update">
    <form method="post" class="form-horizontal" enctype="multipart/form-data">
    
            <!--In order to prevent Malicious attacks, we will test the CSRF token in the session against a value in a hidden input. If they match, the code will execute. If not, the code will exit. -->
            <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">

<table id="Files">
<?php 
$id=intval($_GET['update_id']);
$stmt1 = "SELECT proj.id, proj.title, proj.beginning, proj.ending, proj.supv_ext, proj.comment, proj.supvid, proj.supvid2, proj.supvid3, stat.surname, stat.firstname, stat.matriculation, supvid_supervisors.supvname as sname1, supvid2_supervisors.supvname as sname2, supvid3_supervisors.supvname as sname3, stat.matriculation as studid, cat.categoryname, cat.id as catid, institute.instit,  institute.id as institid, documents.docname,  documents.id as docid,  process.proc, process.id as procid FROM studentprojects proj LEFT JOIN studentsandauthors stat ON proj.studid=stat.matriculation LEFT JOIN projcategory cat ON proj.catid=cat.id LEFT JOIN institute ON proj.institid=institute.id LEFT JOIN documents ON proj.docid=documents.id LEFT JOIN process ON proj.procid=process.id LEFT JOIN supervisors supvid_supervisors on proj.supvid=supvid_supervisors.id LEFT JOIN supervisors supvid2_supervisors on proj.supvid2=supvid2_supervisors.id LEFT JOIN supervisors supvid3_supervisors on proj.supvid3=supvid3_supervisors.id where proj.id=:id ";

//Creates prepared statement
$query1 = $pdo -> prepare($stmt1);
$query1->bindParam(':id',$id,PDO::PARAM_STR);
//Executes the query
$query1->execute();
//Sets fetch mode to the given parameter
$results=$query1->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query1->rowCount() > 0)
{
foreach($results as $result)
{               ?>  
 <input type='hidden' value='<?php echo htmlentities($result->studid);?>' name='studid' >
<?php }} ?>

    <?php
        if(isset($errorMsg))
        {
            ?>
            <div class="alert alert-danger">
                <strong><span style="color:red">FEHLER !</span> <?php echo $errorMsg; ?></strong>
            </div>
            <?php
        }
        if(isset($insertMsg)){
        ?>
            <div class="alert alert-success">
                <strong><span style="color:red">ERFOLGREICH !</span>  <?php echo $insertMsg; ?></strong>
            </div>
        <?php
        }
        ?>   


                    

                        
                        <p>
                    <div class="form-group">
                    <label class="col-sm-3 control-label">Erlaubte Formate: pdf, jpg, jpeg, png, gif</label>
                    <div class="col-sm-6">
                    <input type="file" name="file_one" class="form-control" value="<?php echo $file_path1; ?>"/>
                    <input type="submit"  name="btn_update" class="btn btn-primary" value="Update">
                    
                    <?php if(empty($row['file_path1'])){  ?>
                        <img src="inc/platzhalter.png" alt="Leer" width="200" height="200">
                    <?php }else{ ?>
                        <embed src="upload/<?php echo($row['file_path1']);?>" type="application/pdf" frameBorder="0" scrolling="auto" height="1200px"width="1400px"></td>
                    <?php } ?>
                    </div>
                    </div>

                    </p>
                </form>
    
</table>
</div>

标签: phpfileupload

解决方案


首先,在第 4 行,确保您的 POST 有价值的是正确的。其次,第 23 行,您试图从 POST 全局变量而不是数据库中获取 studid。您需要使用 $row[0]["studid"];


推荐阅读