无法使用 gd 库 php 上传图像
Posted
技术标签:
【中文标题】无法使用 gd 库 php 上传图像【英文标题】:unable to upload an image using gd library php 【发布时间】:2015-05-20 14:31:10 【问题描述】:此 php 文件从用户从计算机上传图像的 html 文件接收数据,一旦单击更新按钮,此 php 页面就会返回错误,指出不存在此类文件或目录
<?php error_reporting(E_ALL); ini_set('display_errors', 1);?>
<?php
$db = mysql_connect('localhost', 'pippo', 'pluto') or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db('moviesite', $db) or die(mysql_error($db));
// current images folder
$dir ='image_php/images';
// make sure the upload succeeded
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;
// retrieve data from created image
$image_caption = $_POST['caption'];
$image_username = $_POST['username'];
$image_date = date('Y-m-d');
list($width, $height, $type, $attr) =
getimagesize($_FILES['uploadfile']['tmp_name']);
// make sure the uploaded file is a supported image
switch ($type)
case IMAGETYPE_GIF:
$image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or
die('The file you uploaded was not a supported filetype');
$ext = '.gif';
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or
die('The file you uploaded was not a supported filetype');
$ext = '.jpg';
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) 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');
//insert information into image table
$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));
//retrieve image value
$last_id = mysql_insert_id();
// use id as image name
//per assicurarsi che l'immagine non sovrascriva altre immagini esistenti
$imagename = $last_id . $ext;
// update image table adding the image final name
$query = 'UPDATE images
SET image_filename = "' . $imagename . '"
WHERE image_id = ' . $last_id;
$result = mysql_query($query, $db) or die (mysql_error($db));
// save image
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);
?>
<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>
这是页面返回的错误:
警告:imagejpeg():无法打开“image_php/images/9.jpg”进行写入:第 98 行的 /var/www/html/php/image_php/check_image.php 中没有此类文件或目录
允许上传图片的表单
<html>
<head>
<title>Upload your pic to our site!</title>
<style type="text/css">
<!--
td vertical-align: top;
-->
</style>
</head>
<body>
<form action="check_image.php" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Your Username</td>
<td><input type="text" name="username" /></td>
</tr>
<td>Upload Image*</td>
<td><input type="file" name="uploadfile" /></td>
</tr><tr>
<td colspan="2">
<small><em>* Acceptable image formats include: GIF, JPG/JPEG and PNG.
</em></small>
</td>
</tr><tr>
<td>Image Caption<br/>
</td>
<td><input type="text" name="caption" /></td>
</tr><tr>
<td colspan="2" style="text-align: center;">
<input type="submit" name="submit" value="Upload"/>
</td>
</tr>
</table>
</form>
</body>
</html>
【问题讨论】:
处理文件时需要一个有效的enctype,你没有用吗? 是的,我在允许上传图片的 html 文件中使用了有效的 enctype 然后回答下面给出的答案 刚刚添加了@Fred-ii- 确保您希望写入的文件夹可以被写入。检查权限。似乎是这里最有可能的问题/解决方案;那就是关于警告的内容。 【参考方案1】:来自 PHP 手册:
http://php.net/manual/en/features.file-upload.post-method.php
默认情况下,文件将存储在服务器的默认临时目录中,除非在 php.ini 中使用 upload_tmp_dir 指令指定了另一个位置。可以通过在 PHP 运行的环境中设置环境变量 TMPDIR 来更改服务器的默认目录。在 PHP 脚本中使用 putenv() 设置它不起作用。此环境变量还可用于确保其他操作也在处理上传的文件。
可能您的 HTTP 服务器没有临时目录的写入权限,或者您的 php.ini 为没有写入权限的目录配置了临时目录。 检查它们。
【讨论】:
我应该创建一个临时文件夹吗? 没有。您应该检查您的 php.ini 并查看那里设置了哪个临时目录。然后你检查你的服务器用户是否有这个目录的写权限。 我正在使用设置为开发环境的 Web 服务器一种我创建的镜像服务器它不是生产服务器 是的,甚至更好。检查你的 php.ini 中是否有包含upload_tmp_dir 的行。如果已启用,这是您必须检查权限的目录。如果它被禁用然后在系统默认。在大多数 linux 发行版中,这将是 /tmp。您的 http 服务器用户(如果是默认配置)应该是 www-data。因此,考虑到默认配置,您应该检查 www-data 是否对 /tmp 具有写入权限。 我正在检查你所说的那一行中的 php.ini,但我无法说明它是否被禁用以上是关于无法使用 gd 库 php 上传图像的主要内容,如果未能解决你的问题,请参考以下文章