php – move_uploaded_file返回false,但$_FILES [‘uploadedfile’] [‘error’] == 0

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – move_uploaded_file返回false,但$_FILES [‘uploadedfile’] [‘error’] == 0脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用POST方法在upload_photo.PHP文件上传照片,该文件使用uploader.PHP来处理图像. Uploader.PHP调整图像大小并覆盖旧图像.它在本地工作正常,但不在服务器上.

move_uploaded_file返回false,但是$_FILES [‘uploadeDFile’] [‘error’] == 0这对我来说没有.

我已经发布了整个uploader.PHP和来自upload_photo.PHP的片段,其中显示了表单标签.

<?PHP
//This is uploader.PHP
session_start();
include ('dbconfig.PHP');
include('Simpleimage.PHP');

MysqL_connect(HOST,USERNamE,PASSWORD);
$conn = MysqL_select_db(DB_NAME);

$target_path = "uploads/";

$target_path = $target_path . renameImage();

$_SESSION['client_photo'] = $target_path;

$query = "UPDATE client ";
$query .= "SET PErsonal_photo = ('$target_path') ";
$query .= "WHERE client_id = ".$_SESSION['clientID'];
$results = MysqL_query($query) or die('Error msg: '.MysqL_error().'<br/>
        sql: '.query.'<br/>');

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path)) {
  $msg = "The file ".  $_SESSION['client_photo']. 
  " has been uploaded";
  chmod($target_path,"0666");
  $work = new imgResizer($target_path); 
  $work -> resize(600,$target_path); 
} else{
  $msg = "There was an error uploading the file,please try again!";
  $msg .= $_FILES['uploadedfile']['error'];
}
header("Location: upload_photo.PHP?msg=$msg");

function renameImage(){ 
  MysqL_connect(HOST,PASSWORD);
  $conn = MysqL_select_db(DB_NAME);

  $sql = "SELECT First_name,last_name,client_id
          From client
          WHERE client_id = ".$_SESSION['clientID'];
  $res = MysqL_query($sql) or die('Error msg: '.MysqL_error().'<br/>
                sql: '.$sql.'<br/>');
  if($row = MysqL_fetch_array($res,MysqLI_ASSOC)){
      $_SESSION['successphoto'] = 1;
      return $row{first_name}.'_'.$row{last_name}.'_'.$row{client_id};
  }
  else{
      echo "There was an error while fetching the row.<br/>";
  }
}

class ImgResizer {
  PRivate $originalFile = '';
  public function __construct($originalFile = '') {
      $this -> originalFile = $originalFile;
  }
  public function resize($newWidth,$targetFile) {
      if (empty($newWidth) || empty($targetFile)) {
          return false;
      }
      $src = imagecreatefromjpeg($this -> originalFile);
      list($width,$height) = getimagesize($this -> originalFile);
      $newHeight = ($height / $width) * $newWidth;
      $tmp = imagecreatetruecolor($newWidth,$newHeight);
      imagecopyresampled($tmp,$src,$newWidth,$newHeight,$width,$height);
      if (file_exists($targetFile)) {
          unlink($targetFile);
      }
      imagejpeg($tmp,$targetFile,85); // 85 is my choice,make IT between 0 – 100 for output image quality with 100 being the most luxurIoUs
  }
}

?>

以下是upload_photo.PHP的片段

echo '<form enctype="multipart/form-data" action="uploader.PHP" method="POST">';
echo '<tr>';
//echo "<td colspan='2'>".$_GET['text']."</td>";
echo '</tr>';
echo '<tr align="center">';
echo '<td colspan="2">';
echo '<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
        Choose a file to upload: <input name="uploadedfile" type="file" accept="image/*"/><br />
        <input type="submit" value="Upload File" />
      </form>';

解决方法

这可能意味着上传运行正常 – 文件上传到临时目录 – 但文件无法移动到您设置的目标.

应该仔细检查要将文件移动到的路径,并确保Web用户(apache,www或其他)有权写入该目录.

脚本宝典总结

以上是脚本宝典为你收集整理的php – move_uploaded_file返回false,但$_FILES [‘uploadedfile’] [‘error’] == 0全部内容,希望文章能够帮你解决php – move_uploaded_file返回false,但$_FILES [‘uploadedfile’] [‘error’] == 0所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。