文件上传没有任何理由

时间:2010-03-20 10:26:46

标签: php file-upload

喂 我想要上传文件。脚本应该拍摄图像,调整大小并上传。 但似乎上传中有任何未知的错误。

这里是代码

define ("MAX_SIZE","2000");     // maximum size for uploaded images
  define ("WIDTH","107"); // width of thumbnail
  define ("HEIGHT","107"); // alternative height of thumbnail (portrait 107x80)
  define ("WIDTH2","600"); // width of (compressed) photo
  define ("HEIGHT2","600"); // alternative height of (compressed) photo (portrait 600x450)

  if (isset($_POST['Submit'])) {
    // iterate thorugh all upload fields
    foreach ($_FILES as $key => $value) {

      //read name of user-file
      $image = $_FILES[$key]['name'];
      // if it is not empty
      if ($image) {
        $filename = stripslashes($_FILES[$key]['name']);  // get original name of file from clients machine
        $extension = getExtension($filename); // get extension of file in lower case format
        $extension = strtolower($extension);

        // if extension not known, output error
        // otherwise continue
        if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
          echo '<div class="failure">Fehler bei Datei '. $_FILES[$key]['name'] .': Unbekannter Dateityp: Es können nur Dateien vom Typ .gif, .jpg oder .png hochgeladen werden.</div>';
        } else {
          // get size of image in bytes
          // $_FILES[\'image\'][\'tmp_name\'] >> temporary filename of file in which the uploaded file was stored on server
          $size = getimagesize($_FILES[$key]['tmp_name']);
          $sizekb = filesize($_FILES[$key]['tmp_name']);

          // if image size exceeds defined maximum size, output error
          // otherwise continue
          if ($sizekb > MAX_SIZE*1024) {  
            echo '<div class="failure">Fehler bei Datei '. $_FILES[$key]['name'] .': Die Datei konnte nicht hochgeladen werden: die Dateigröße überschreitet das Limit von 2MB.</div>';
          } else {
            $rand = md5(rand() * time());  // create random file name
            $image_name = $rand.'.'.$extension;   // unique name (random number)

            // new name contains full path of storage location (images folder)
            $consname = "photos/".$image_name;  // path to big image
            $consname2 = "photos/thumbs/".$image_name;  // path to thumbnail

            $copied = copy($_FILES[$key]['tmp_name'], $consname);
            $copied = copy($_FILES[$key]['tmp_name'], $consname2);

            $sql="INSERT INTO photos (galery_id, photo, thumb) VALUES (". $id .", '$consname', '$consname2')" or die(mysql_error());
            $query = mysql_query($sql) or die(mysql_error());

            // if image hasnt been uploaded successfully, output error
            // otherwise continue
            if (!$copied) {
              echo '<div class="failure">Fehler bei Datei '. $_FILES[$key]['name'] .': Die Datei konnte nicht hochgeladen werden.</div>';
            } else {
              $thumb_name = $consname2;   // path for thumbnail for creation & storage
              // call to function: create thumbnail
              // parameters: image name, thumbnail name, specified width and height
              $thumb = make_thumb($consname,$thumb_name,WIDTH,HEIGHT);
              $thumb = make_thumb($consname,$consname,WIDTH2,HEIGHT2);
            }
          }
        }
      }
    }
    // current image could be uploaded successfully
    echo '<div class="success">'. $success .' Foto(s) erfolgreich hochgeladen!</div>';
    showForm();   // call to function: create upload form 
  }

1 个答案:

答案 0 :(得分:1)

我们能看到PHP错误日志吗?


实际上是这样的信息: 'photos / 03be646900419daa11eaa1d1af1fd024.jpg'不是有效的JPEG文件

这么说!该文件不是有效的JPEG,这就是问题所在。