douneiben2240 2017-04-15 09:13
浏览 143
已采纳

PHP + MYSQL:错误move_uploaded_file()无法移动

I'm stucked in one place, I have a file upload php file but when I want to upload the image I have errors called:
"Warning: move_uploaded_file(): failed to open stream: No such file or directory.." and
"Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php7E6D.tmp' to 'site/images/UBI30_Giveaway_box_CARDS_1.jpg'.. "
My Upload php looks like this:

//Image datas
$target_dir = "site/images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Submint button pressed?
if(isset($_POST['submit'])){
  $nev=$_POST['nev'];
  $about=$_POST['about'];
  $image=$_FILES["fileToUpload"]["name"];
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

// Does it image?
      if($check !== false) {
          echo "File is an image - " . $check["mime"] . ".";
          $uploadOk = 1;
      } else {
          echo "File is not an image.";
          $uploadOk = 0;
      }

//Recently the image is uploaded?
      if (file_exists($target_file)) {
      echo "Sajnáljuk, ez már egy feltöltött fájl..";
      $uploadOk = 0;
      }

//If everything okay, upload.
      if ($uploadOk == 0) {
      echo "Sajnáljuk a kép nem került feltöltésre..";
      } else {
          if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
              echo "A ". basename( $_FILES["fileToUpload"]["name"]). " feltöltve.";
          } else {
              echo "Sajnáljuk, hiba történt a kép feltöltése közben.";
          }
      }
  }
// Upload datas to db.
  $stmt = $db->prepare("INSERT INTO `jelolt`(`name`, `about`, `image`) VALUES ('$nev','$about','$image')");
    $stmt->execute();
// A lekérdezés helyes futásának ellenörzése.
      if (!$stmt) {
        print_r($db->errorInfo());
      }  

I hope you guys can be clever than me, I'm just don't find my bug around 3 hours ago.

UPDATE with $target_dir = getcwd(); modify this start work but not put the correct folder, and if I change the url I got the same problem . I need to attach /views/site/image url after getcwd() but of corse it's doesn't work for me: $target_dir = getcwd()."views/site/image";

  • 写回答

2条回答 默认 最新

  • dongyue0263 2017-04-15 11:05
    关注

    getcwd() returns your current working directory. If you add views/site/image to it, you will get the folder where your page is in + the path you expect. You need a slash at the end as well, like views/site/image. If your page is not inside the root folder and it is fairly safe to assume this as your current case, for instance, this is a page called foo/bar, then your file will be tried to be uploaded into the foo folder, however, your views folder is probably in your web root. So you need to define the position compared to root, like this: dirname(__FILE__)."views/site/image/".$yourfilename. Make sure you have the necessary privileges for all the folders in the path.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题