无法设置权限使用PHP GD库上传图片 [英] unable to set permissions to upload images using php gd library

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

问题描述

这是一个类似的问题,我问过一些天前在这里你可以找到所有的源$ C ​​$的c您可能需要
<一href=\"http://stackoverflow.com/questions/30352700/unable-to-upload-an-image-using-gd-library-php/30352891?noredirect=1#comment48805182_30352891\">unable使用GD库的PHP 上传图片

在我的php.ini我已经更新 upload_tmp_dir upload_tmp_dir = / tmp目录
open_base_dir open_base_dir = / tmp目录

不管怎样,每当我试着上传文件错误报告返回这个错误:


  

警告:imagejpeg():无法打开,无法写入image_php /图像/ 18.jpg:在/var/www/html/php/image_php/check_image.php在线98没有这样的文件或目录


由于事实上,我仍然不知道它是否是许可的问题,但我相信这是,我无法上传图片

下面是PHP源$ C ​​$ C用来处理提交的文件
这应该是无论如何足够多的该文件来自一个HTML表单后,用户不上传图片,从他/她的本地文​​件夹中选择根据他/她的PC

\r
\r

&LT; PHP的error_reporting(E_ALL);的ini_set('的display_errors',1);?&GT;\r
&LT; PHP\r
$ DB = mysql_connect('localhost'的,'傻瓜','唐老鸭')或\r
    死亡(无法连接检查您的连接参数。');\r
mysql_select_db('moviesite',$分贝)或死亡(mysql_error($分贝));\r
\r
// cambiare questo percorso在摩多车corrisponda一克约德拉cartella\r
//在USO图片\r
$ DIR ='image_php /图片;\r
\r
// SI assicura澈IL caricamento SIA avvenuto correttamente\r
如果($ _FILES ['uploadfile'] ['错误']!= UPLOAD_ERR_OK){\r
开关($ _FILES ['uploadfile'] ['错误']){\r
案例UPLOAD_ERR_INI_SIZE:\r
死(上传的文件超过了upload_max_filesize的指令。\r
在php.ini');\r
突破;\r
案例UPLOAD_ERR_FORM_SIZE:\r
死(上传的文件超过了MAX_FILE_SIZE指令,'。\r
在HTML表单指定。');\r
突破;\r
案例UPLOAD_ERR_PARTIAL:\r
死('上传的文件只有部分被上传。');\r
突破;\r
案例UPLOAD_ERR_NO_FILE:\r
死('没有文件被上传');\r
突破;\r
案例ULOAD_ERR_NO_TMP_DIR:\r
死('服务器缺少临时文件夹');\r
突破;\r
案例UPLOAD_ERR_CANT_WRITE:\r
死(服务器无法写入上传的文件到硬盘');\r
突破;\r
案例UPLOAD_ERR_EXTENSION:\r
死('文件上传推而广之停止。');\r
突破;\r
}\r
}\r
\r
$ IMAGE_FILE的tempnam =($目录,上载);\r
如果(move_uploaded_file($ _ FILES ['uploadfile'] ['tmp_name的值'],$ IMAGE_FILE)=真!){死(无法移动上传的文件!); }\r
\r
// recupera乐informazioni sull'immagine appena caricata\r
$ image_caption = $ _ POST ['标题'];\r
$ image_username = $ _ POST [用户名];\r
$ image_date =日期('Y-M-D');\r
列表($宽度,高度$,$型,$ ATTR)=\r
    和getimagesize($ IMAGE_FILE);\r
\r
// SI assicura澈IL文件caricato SIA effettivamente联合国TIPO迪想象,\r
// supportato\r
开关($型){\r
案例IMAGETYPE_GIF:\r
     $图像= imagecreatefromgif($ IMAGE_FILE)或\r
死('你上传的文件是不支持的文件类型);\r
$ EXT ='.gif注意';\r
突破;\r
案例IMAGETYPE_JPEG:\r
    $图像= imagecreatefromjpeg($ IMAGE_FILE)或\r
死('你上传的文件是不支持的文件类型);\r
$ EXT =名为.jpg;\r
突破;\r
案例IMAGETYPE_PNG:\r
   $图像= imagecreatefrompng($ IMAGE_FILE)或\r
        死('你上传的文件是不支持的文件类型);\r
   $ EXT ='巴纽;\r
   打破;\r
默认:\r
    死('你上传的文件是不支持的文件类型);\r
}\r
\r
// inserisce内拉tabella图像文件informazioni\r
$查询=INSERT INTO图片\r
    (image_caption,image_username,image_date)\r
VALUES\r
    ('。$ image_caption','。$ image_username','。$ image_date。\r
')';\r
$结果= mysql_query($查询,$分贝)或死亡(mysql_error($分贝));\r
\r
// recupera IL valore image_id车MYSQL公顷generatp automaticamente quando\r
// abbiamo inserito乐informazioni sull'immagine德拉tabella\r
//金正日诺沃纪录\r
$ last_id = mysql_insert_id();\r
\r
//拿督澈身份证电子univoco罗州市璞utilizzare anche来诺姆dell'immagine\r
//每assicurarsi车L'想象,非sovrascriva altre immagini esistenti\r
$ imagename = $ last_id。 $ EXT;\r
\r
// aggiorna拉tabella图像山坳诺姆压轴dell'immagine\r
$查询=更新映像\r
    SET IMAGE_FILENAME ='。$ imagename'。\r
    WHERE image_id ='。 $ last_id;\r
$结果= mysql_query($查询,$分贝)或死亡(mysql_error($分贝));\r
\r
//萨尔瓦L'想象,德拉SUA destinazione压轴\r
开关($型){\r
案例IMAGETYPE_GIF:\r
    imagegif(。$形象,$ DIR'/'$ imagename);\r
突破;\r
案例IMAGETYPE_JPEG:\r
    imagejpeg($图像,$ DIR'/'$ imagename,100);\r
突破;\r
案例IMAGETYPE_PNG:\r
    imagepng($图像,$ DIR'/'$ imagename);\r
突破;\r
}\r
imagedestroy($图像);\r
取消链接($ IMAGE_FILE);\r
?&GT;\r
&LT; HTML和GT;\r
  &LT; HEAD&GT;\r
    &LT;标题&gt;此处是你的图片&LT;!/标题&GT;\r
  &LT; /头&GT;\r
 &LT;身体GT;\r
   &LT; H1&GT;那么它是怎样的感觉是著名&LT;?/ H1&GT;\r
   &LT; P&gt;此处是你刚刚上传到我们的服务器图片:LT; / P&GT;\r
    &LT; IMG SRC =图像/&LT; PHP的echo $ imagename;&gt;中风格=浮动:左;&GT;\r
&LT;表&gt;\r
&LT; TR&GT;&LT; TD&GT;图像保存为:LT; / TD&GT;&LT; TD&GT;&LT; PHP的echo $ imagename; ?&GT;&LT; / TD&GT;&LT; / TR&GT;\r
&LT; TR&GT;&LT; TD&GT;图像类型:?&LT; / TD&GT;&LT; TD&GT;&LT; PHP的echo $ EXT; ?&GT;&LT; / TD&GT;&LT; / TR&GT;\r
&LT; TR&GT;&LT; TD&GT;身高:&LT; / TD&GT;&LT; TD&GT;&LT; PHP的回声$高度; ?&GT;&LT; / TD&GT;&LT; / TR&GT;\r
&LT; TR&GT;&LT; TD&GT;宽度:&LT; / TD&GT;&LT; TD&GT;&LT; PHP的echo $宽度; ?&GT;&LT; / TD&GT;&LT; / TR&GT;\r
&LT; TR&GT;&LT; TD&GT;上传日期:&LT; / TD&GT;&LT; TD&GT;&LT; PHP的echo $ image_date; ?&GT;&LT; / TD&GT;&LT; / TR&GT;\r
   &LT; /表&gt;\r
 &LT; /身体GT;\r
 &LT; / HTML&GT;

\r

\r
\r我说如果条件到code的建议
但我们仍然从那里,我们开始从得到的:在网页的作品我每次需要的参数,但图像无法加载

警告:imagejpeg():无法打开,无法写入image_php /图像/ 20.jpg:在/var/www/html/php/image_php/check_image.php线101没有这样的文件或目录


解决方案

添加之前对不对获取图像数据

  $ IMAGE_FILE的tempnam =($目录,上载);
move_uploaded_file($ _ FILES ['uploadfile'] ['tmp_name的值'],$ IMAGE_FILE);

在进一步code替换 $ _ FILES ['uploadfile'] ['tmp_name的值'] $ IMAGE_FILE

和后 imagedestroy()补充:

 取消链接($ IMAGE_FILE);

This is a similar question I asked some days ago here you can find all of the source code you might need unable to upload an image using gd library php

in my php.ini I've updated upload_tmp_dir writing upload_tmp_dir = /tmp and open_base_dir to open_base_dir = /tmp

Anyways whenever I try to upload a file error reporting returns this bug:

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

As a matter of fact I am still unaware whether or not it is a problem of permission but I do believe it is and I am unable to upload images

here is the source code of the php which handles the submission of the file this should be enought anyway this file comes after an html form where the user does upload the image choosing from his/her local folder based on his/her pc

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

I added the if condition into the code as suggested but still we get back from where we started from: the page works I have every parameters I need but the image cannot be loaded

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

解决方案

Add right before // retrieve image data:

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

Replace in further code all occurrences of $_FILES['uploadfile']['tmp_name'] to $image_file

And after imagedestroy() add:

unlink($image_file);

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

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