【问题标题】:unable to set permissions to upload images using php gd library无法使用 php gd 库设置上传图片的权限
【发布时间】:2015-05-22 09:11:05
【问题描述】:

这是我几天前问的一个类似问题,在这里你可以找到你可能需要的所有源代码 unable to upload an image using gd library php

在我的 php.ini 中,我更新了 upload_tmp_dir,写成 upload_tmp_dir = /tmpopen_base_diropen_base_dir = /tmp

无论如何,每当我尝试上传文件时,错误报告都会返回此错误:

警告:imagejpeg():无法打开“image_php/images/18.jpg”进行写入:第 98 行的 /var/www/html/php/image_php/check_image.php 中没有此类文件或目录

事实上,我仍然不知道这是否是许可问题,但我确实相信这是并且我无法上传图片

这里是处理文件提交的php的源代码 无论如何,这应该足够了,这个文件来自一个 html 表单,用户根据他/她的电脑上传从他/她的本地文​​件夹中选择的图像

<?php error_reporting(E_ALL); ini_set('display_errors', 1);?>
<?php 
$db = mysql_connect('localhost', 'goofy', 'donald') or 
    die ('Unable to connect. Check your connection parameters.');
mysql_select_db('moviesite', $db) or die(mysql_error($db));

// cambiare questo percorso in modo che corrisponda a quello della cartella
// images in uso
$dir ='image_php/images';

// si assicura che il caricamento sia avvenuto correttamente
if ($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK) {
	switch ($_FILES['uploadfile']['error']) {
	case UPLOAD_ERR_INI_SIZE:
	    die('The uploaded file exceeds the upload_max_filesize directive' .
		    'in php.ini');
		break;
	case UPLOAD_ERR_FORM_SIZE:
	    die('The uploaded file exceeds the MAX_FILE_SIZE directive that ' .
		    'was specified in the HTML form.');
		break;
	case UPLOAD_ERR_PARTIAL:
	    die('The uploaded file was only partially uploaded.');
		break;
	case UPLOAD_ERR_NO_FILE:
	    die('No file was uploaded');
		break;
	case ULOAD_ERR_NO_TMP_DIR:
	    die('The server is missing a temporary folder');
		break;
	case UPLOAD_ERR_CANT_WRITE:
	    die('The Server failed to write the uploaded file to disk');
		break;
	case UPLOAD_ERR_EXTENSION:
	    die('File upload stopped by extension.');
		break;
	}
}

$image_file = tempnam($dir,"upload");
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $image_file)!=true) { die("Can't move uploaded file!"); }

// recupera le informazioni sull'immagine appena caricata
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = date('Y-m-d');
list($width, $height, $type, $attr) =
    getimagesize($image_file);

// si assicura che il file caricato sia effettivamente un tipo di immagine
// supportato
switch ($type) {
case IMAGETYPE_GIF:
     $image = imagecreatefromgif($image_file) or 
	     die('The file you uploaded was not a supported filetype');
	$ext = '.gif';
	break;
case IMAGETYPE_JPEG:
    $image = imagecreatefromjpeg($image_file) or 
	    die('The file you uploaded was not a supported filetype');
	$ext = '.jpg';
	break;
case IMAGETYPE_PNG:
   $image = imagecreatefrompng($image_file) or 
        die('THe file you uploaded was not a supported filetype');
   $ext = '.png';
   break;
default:
    die('The file you uploaded was not a supported filetype');
}

// inserisce nella tabella images le informazioni
$query = 'INSERT INTO images
    (image_caption, image_username, image_date)
VALUES
    ("' . $image_caption . '", "' . $image_username . '", "' . $image_date .
	 '")';
$result = mysql_query($query, $db) or die(mysql_error($db));

//recupera il valore image_id che MYSQL ha generatp automaticamente quando
// abbiamo inserito le informazioni sull'immagine della tabella
// il nuovo record
$last_id = mysql_insert_id();

// dato che id è univoco lo si può utilizzare anche come nome dell'immagine
//per assicurarsi che l'immagine non sovrascriva altre immagini esistenti
$imagename = $last_id . $ext;

// aggiorna la tabella images col nome finale dell'immagine
$query = 'UPDATE images
    SET image_filename = "' . $imagename . '"
    WHERE image_id = ' . $last_id;
$result = mysql_query($query, $db) or die (mysql_error($db));

// salva l'immagine della sua destinazione finale
switch ($type) {
case IMAGETYPE_GIF:
    imagegif($image, $dir . '/' . $imagename);
	break;
case IMAGETYPE_JPEG:
    imagejpeg($image, $dir . '/' . $imagename, 100);
	break;
case IMAGETYPE_PNG:
    imagepng($image, $dir . '/' . $imagename);
	break;
}
imagedestroy($image);
unlink($image_file);
?>
<html>
  <head>
    <title>Here is your pic!</title>
  </head>
 <body>
   <h1>So how does it feel to be famous?</h1>
   <p>Here is the picture you just uploaded to our servers:</p>
    <img src="images/<?php echo $imagename; ?>" style="float:left;">
	<table>
	<tr><td>Image Saved as: </td><td><?php echo $imagename; ?></td></tr>
	<tr><td>Image Type: </td><td><?php echo $ext; ?></td></tr>
	<tr><td>Height: </td><td><?php echo $height; ?></td></tr>
	<tr><td>Width: </td><td><?php echo $width; ?></td></tr>
	<tr><td>Upload Date: </td><td><?php echo $image_date; ?></td></tr>
   </table>
 </body>
 </html>
我按照建议将 if 条件添加到代码中 但我们仍然从我们开始的地方回来:页面工作我有我需要的所有参数但无法加载图像

警告:imagejpeg():无法打开“image_php/images/20.jpg”进行写入:第 101 行的 /var/www/html/php/image_php/check_image.php 中没有此类文件或目录

【问题讨论】:

标签: php html apache


【解决方案1】:

// retrieve image data之前添加:

$image_file = tempnam($dir,"upload");
move_uploaded_file($_FILES['uploadfile']['tmp_name'], $image_file);

将所有出现的$_FILES['uploadfile']['tmp_name'] 替换为$image_file

imagedestroy()之后添加:

unlink($image_file);

【讨论】:

  • 我怀疑,脚本无权访问上传的文件。 if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $image_file)!=true) { die("Can't move uploaded file!"); }
  • 我必须在哪里添加这个?我要替换你建议我的现有线路吗?
  • 是的,这只是move_uploaded_file函数处理错误的更新。
  • 我已经用新的替换了现有的 move_upload 部分,但没有任何改变,可能脚本无法访问上传的文件
  • 问题已被编辑了第一百次,但仍然没有任何改变,我找不到解决此错误的解决方案
猜你喜欢
  • 1970-01-01
  • 2015-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
相关资源
最近更新 更多