我有下面的代码,没有上传图像试过很多东西,但不起作用。我无法弄清楚问题是什么,但是在日志之前使用了相同的代码。
HTML表单
<form method="post" action="" enctype="multipart/form-data" name="form1">
<input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/>
<input type="submit" value="Upload" name="submit"/>
</form>
最后一次尝试代码如下:
$change="";
$abc="";
define ("MAX_SIZE","12000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}else{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024){
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}else {
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=1990;
$newheight=1100;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$imu = mysql_query("select max(id)+1 as id from menu_items");
$ints = mysql_fetch_array($imu);
$chk = $ints['id'];
$ids = $ints['id'];
$srids = $ints['showroom_id'];
$name = $_FILES["file"]["name"];
$ext = end((explode(".", $name))); # extra () to prevent notice
$ses_msr = $_SESSION['ses_member_id'];
$folderPath = "images/rest_menu_items";
mkdir($folderPath);
$filename = "images/rest_menu_items/".$ids.'.'.$ext;
$filename1 = "images/small". $_FILES['file']['name'];
imagejpeg($tmp,$filename,100);
mysql_query("
UPDATE menu_items
SET
filename='".$ids.'.'.$ext."'
WHERE
id='".$_GET['id']."'
");
imagedestroy($src);
imagedestroy($tmp);
}}
}
答案 0 :(得分:0)
只需更改Function
语法即可。在function
关键字
function getExtension($str){
$i=strrpos($str,".");
if(!$i){return"";}
$l=strlen($str)-$i;
$ext=substr($str,$i+1,$l);
return$ext;
}
在代码下方使用错误。
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
答案 1 :(得分:0)
解决这些问题:
第6行:
function getExtension($str){
第48行:
echo $src;
第79行:
mkdir($folderPath);
并确保您具有写入权限。
PHP错误日志也会有很大帮助。如果你放
,你可以调试你的代码error_reporting( E_ALL );
到文件的开头。
还要确保已安装PHP GD。
创建仅包含内容的文件。
<?php
phpinfo(INFO_MODULES);
?>
应该有“ GD支持已启用”如果找不到,则需要安装it。
如果存在,则检查write permissions,文件test.txt必须存在。因此,创建空文件test.txt来测试它。
$filename = 'test.txt';
if (is_writable($filename)) {
echo 'The file is writable';
} else {
echo 'The file is not writable';
}
如果检查了这些内容,则以下代码将起作用。
经过测试的代码。我删除了查询和SESSION数据,因为我无法调试它们而没有所有代码。
<?php
$change="";
$abc="";
define ("MAX_SIZE","12000");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if($_SERVER["REQUEST_METHOD"] == "POST"){
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}else{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024){
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}else if($extension=="png"){
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}else {
$src = imagecreatefromgif($uploadedfile);
}
echo $src;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=1990;
$newheight=1100;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagejpeg($tmp,"Test.jpg",100);
imagedestroy($src);
imagedestroy($tmp);
}}
}
?>