【发布时间】:2015-05-22 09:11:05
【问题描述】:
这是我几天前问的一个类似问题,在这里你可以找到你可能需要的所有源代码 unable to upload an image using gd library php
在我的 php.ini 中,我更新了 upload_tmp_dir,写成 upload_tmp_dir = /tmp
和open_base_dir 到open_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>
警告:imagejpeg():无法打开“image_php/images/20.jpg”进行写入:第 101 行的 /var/www/html/php/image_php/check_image.php 中没有此类文件或目录
【问题讨论】:
-
向我们展示您将上传文件移动到
image_php/images/18.jpg的代码。您的警告与上传无关。 -
@umka 我刚刚更新了我的问题,请查看
-
你必须move_uploaded_file才能打开它。
-
@umka 哥们,你能给我一个答案吗?
-
看这里[PHP上传文件][1][1]:php.net/manual/en/features.file-upload.post-method.php