Move_upload_file()无法打开流并且无法移动文件

时间:2017-01-07 04:48:45

标签: php

在将图像文件上传到文件夹中时出现问题,如果文件存在,则按当前上传文件移动... 第一次上传很好,但在移动过程中它显示出一些错误

<?php include_once('../includes/config.php');

   if(isset($_POST['add_code']))
   {
       if (($_FILES['photo']['name']!="")){
           // Where the file is going to be stored
           $target_dir = "manufacturer/";
           $file = $_FILES['photo']['name'];
           $path = pathinfo($file);
           $filename = $path['filename'];
           $ext = $path['extension'];
           $temp_name = $_FILES['photo']['tmp_name'];
           $path_filename_ext = $target_dir.$filename.".".$ext;

           // Check if file already exists
           if (file_exists($path_filename_ext)) {
               echo "Sorry, file already exists.";
           }else{
               move_uploaded_file($temp_name,$path_filename_ext);
               echo "Congratulations! File Uploaded Successfully.";
           }
       }           
       $ititle=$_POST['image_title'];
       $c_=mysqli_query($connection,"INSERT INTO `manufacturer`(`mname`, `mimg`) VALUES ('$ititle','$path_filename_ext')");
//$check_user_data=mysqli_fetch_array($check_user_);
       if($c=true){
        echo '1';
       }
   }
?>

-------------------------------------------- -------显示此错误---------------------------------------- ----------

  

警告:move_uploaded_file(manufacturer / 1.jpg):无法打开   stream:没有这样的文件或目录   C:\ xampp \ htdocs \ cabs_admin \ action \ add_manufacturer_image.php在线   19

     

警告:move_uploaded_file():无法移动   'C:\ xampp \ tmp \ phpC119.tmp'到'manufacturer / 1.jpg'中   C:\ xampp \ htdocs \ cabs_admin \ action \ add_manufacturer_image.php在线   19

1 个答案:

答案 0 :(得分:0)

您必须按照以下方式验证一些要点。

1)确保您的表单包含 enctype =&#34; multipart / form-data&#34;

2)请检查您的根目录,您必须创建文件夹&#34;制造商&#34;

现在我希望你有以下文件

form.php文件

<form name="fileUpload" method="post" id="fileUpload" enctype="multipart/form-data" action="success.php">
    <input type="text" name="image_title">
    <input type="file" name="photo">
    <input type="submit" name="add_code" value="Submit">
</form>

success.php文件

<?php

if(isset($_POST['add_code'])){
    if (($_FILES['photo']['name']!="")){
        // Where the file is going to be stored
        $target_dir = "manufacturer/";
        $file = $_FILES['photo']['name'];
        $path = pathinfo($file);
        $filename = $path['filename'];
        $ext = $path['extension'];
        $temp_name = $_FILES['photo']['tmp_name'];
        $path_filename_ext = $target_dir.$filename.".".$ext;

        // Check if file already exists
        if (file_exists($path_filename_ext)) {
            echo "Sorry, file already exists.";
        }else{
           move_uploaded_file($temp_name,$path_filename_ext);
           echo "Congratulations! File Uploaded Successfully.";
       }
    }

    $ititle=$_POST['image_title'];
    $c_=mysqli_query($connection,"INSERT INTO `manufacturer`(`mname`, `mimg`) VALUES ('$ititle','$path_filename_ext')");
    //$check_user_data=mysqli_fetch_array($check_user_);
    if($c=true){
        echo '1';
    }
}
?>

请尝试使用上面列出的代码,我希望它能正常运行。