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

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.

    已采纳该答案
    打赏 评论
  • dongyou9721 2017-04-15 09:52

    I am not near my tools to test this, but from eyelevel view, it seems that you moving the imagesize response and not the file.

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    

    should probably be:

    if (move_uploaded_file($_FILES["fileToUpload"]["name"], $target_file)) {
    

    I can't see anything else at the moment, other than the variables being mixed up there. It seems your uploaded file will be in "name" not "tmp_name". If no one comes up with an answer by the time I am at my workstation, i will run this through a test.

    打赏 评论

相关推荐 更多相似问题