首页 > 解决方案 > PHP图片上传出错

问题描述

因此,我正在尝试使用 PHP 脚本上传图像,但出现这些错误 ps:file_uploads = On in php.ini

文件是图像 - 图像/jpeg。警告:move_uploaded_file(image/promotions/verso.jpg):无法打开流:第 43 行的 W:\xampp\htdocs\elkssar\functions\upload.php 中没有此类文件或目录警告:move_uploaded_file():无法移动第 43 行 W:\xampp\htdocs\elkssar\functions\upload.php 中的“W:\xampp\tmp\phpCB68.tmp”到“image/promotions/verso.jpg” 抱歉,上传文件时出错。

<?php
$target_dir = "image/promotions/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image
if(isset($_POST["upload"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
  echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

标签: php

解决方案


这是一个愚蠢的错误,因为我与 image/ 不在同一个位置,所以代码行应该是这样的$target_dir = "../image/promotions/";


推荐阅读