无法使用gd library php上传图片 [英] unable to upload an image using gd library php

查看:94
本文介绍了无法使用gd library php上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个php文件接收用户从他的电脑上传图片的html文件中的数据,只要点击更新按钮,这个php页面就会返回一个错误信息,表明不存在这样的文件或目录

p>

 <?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)或die(mysql_error($ db)); //当前图像文件夹$ dir ='image_php / images'; //确保上传成功if($ _FILES ['uploadfile'] ['error ')!= UPLOAD_ERR_OK){switch($ _FILES ['uploadfile'] ['error']){case UPLOAD_ERR_INI_SIZE:die('上传的文件超过了php.ini'中的'upload_max_filesize'指令');打破;大小写UPLOAD_ERR_FORM_SIZE:die('上传的文件超过了在HTML表单中指定'。'的MAX_FILE_SIZE指令');打破;大小写UPLOAD_ERR_PARTIAL:die('上传的文件仅部分上传');打破;大小写UPLOAD_ERR_NO_FILE:die('没有文件上传');打破; ULOAD_ERR_NO_TMP_DIR:die('服务器缺少一个临时文件夹');打破;大小写UPLOAD_ERR_CANT_WRITE:die('服务器无法将上传的文件写入磁盘');打破;大小写UPLOAD_ERR_EXTENSION:die('文件上传以扩展名停止');打破; }} //从创建的图像中检索数据$ image_caption = $ _POST ['caption']; $ image_username = $ _POST ['username']; $ image_date = date('Ym-d'); list($ width,$ height ,$ type,$ attr)= getimagesize($ _ FILES ['uploadfile'] ['tmp_name']); //确保上传的文件是受支持的imageswitch($ type){case IMAGETYPE_GIF:$ image = imagecreatefromgif($ _ FILES ['uploadfile'] ['tmp_name'])或死('您上传的文件不是受支持的文件类型'); $ ext ='.gif'; break; case IMAGETYPE_JPEG:$ image = imagecreatefromjpeg($ _ FILES ['uploadfile'] ['tmp_name'])or die('您上传的文件不是受支持的文件类型'); $ ext ='.jpg'; break; case IMAGETYPE_PNG:$ image = imagecreatefrompng($ _ FILES ['uploadfile'] ['tmp_name'])或死('您上传的文件不是受支持的文件类型'); $ ext ='.png';打破;默认:死('您上传的文件不是支持的文件类型');} / /插入信息到图像表$查询='INSERT INTO图像(image_caption,image_username,image_date)VALUES('。$ image_caption。' ($ query = $ db)或die(mysql_error($ db)); //检索图像值$ last_id(); $ image_username。',''。$ image_date。')'; $ result = mysql_query = mysql_insert_id(); //使用id作为图像名称//每个assicurarsi che l'immagine non sovrascriva altre immagini esistenti $ imagename = $ last_id。 $ ext; //更新映像表添加映像的最终名称$ query ='更新映像SET映像文件名='。$ imagename。'WHERE image_id ='。 $ last_id; $ result = mysql_query($ query,$ db)或die(mysql_error($ db)); //保存imageswitch($ 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);打破;} imagedestroy($图像);?>< HTML> < HEAD> < title>这是您的图片!< / title> < /头> <身体GT; < h1>知名度如何?< / h1> < p>以下是您刚上传至我们服务器的图片:< / p> < img src =images /<?php echo $ imagename;?>风格= 浮动:左; > <表> < tr>< td>图片另存为:< / td>< td><?php echo $ imagename; ?>< / TD>< / TR> < tr>< td>图片类型:< / td>< td><?php echo $ ext; ?>< / TD>< / TR> < tr>< td>高度:< / td>< td><?php echo $ height; ?>< / TD>< / TR> < tr>< td>宽度:< / td>< td><?php echo $ width; ?>< / TD>< / TR> < tr>< td>上载日期:< / td>< td><?php echo $ image_date; ?>< / TD>< / TR> < /表> < /体> < / html>  

这是页面返回的错误:



警告:imagejpeg():无法打开'image_php / images / 9.jpg'写入:在/ var / www / html / php / image_php / check_image.php在线98



允许上传图片的表格



 <$ c < />< html>< head>< title>将您的照片上传到我们的网站< / title>< style type =text / css><! -  td {vertical-align:顶部;}  - >< /风格>< /头><身体GT; < form action =check_image.phpmethod =postenctype =multipart / form-data> <表> < TR> < td>您的使用者名称< / td> < td>< input type =textname =username/>< / td> < / TR> < td>上传图片*< / td> < td>< input type =filename =uploadfile/>< / td> < / TR>< TR> < td colspan =2> < small>< em> *可接受的图片格式包括:GIF,JPG / JPEG和PNG。 < / EM>< /小> < / TD> < / TR>< TR> < td>图片说明< br /> < / TD> < td>< input type =textname =caption/>< / td> < / TR>< TR> < td colspan =2style =text-align:center;> < input type =submitname =submitvalue =Upload/> < / TD> < / TR> < /表> < / form>< / body>< / html>  

h2_lin>解决方案

从这里的php手册:

http://php.net/manual/en/features.file-upload.post-method.php



默认情况下,文件将存储在服务器的默认临时目录中,除非php.ini中的upload_tmp_dir指令已给出另一个位置。可以通过设置环境来更改服务器的默认目录在PHP运行的环境中使用变量TMPDIR,在PHP脚本中使用putenv()来设置它将不起作用,这个环境变量也可以用来确保其他操作在上传的文件上工作。 p>

可能您的http服务器没有写入temp目录的权限,或者您的php.ini有一个临时目录,该目录为没有写入权限的目录配置。
请检查它们。


this php file receives data from a html file where an user uploads an image from his computer, as soon as the update button is clicked this php page returns an error in which it is stated that no such file or directory exists

<?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>

this is the error that the page returns:

Warning: imagejpeg(): Unable to open 'image_php/images/9.jpg' for writing: No such file or directory in /var/www/html/php/image_php/check_image.php on line 98

the form which allows to upload images

<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>

解决方案

From php manual here:

http://php.net/manual/en/features.file-upload.post-method.php

"Files will, by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini. The server's default directory can be changed by setting the environment variable TMPDIR in the environment in which PHP runs. Setting it using putenv() from within a PHP script will not work. This environment variable can also be used to make sure that other operations are working on uploaded files, as well."

Probably your http server hasn't write permission for temp dir or your php.ini has a temp dir configured for a dir where it hasn't write permission. Check them.

这篇关于无法使用gd library php上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆