我正在使用这个PHP代码进行图片上传。但是我一直在接受最后的错误。结果“上传图片时出错。请再试一次。我觉得我已经检查了所有内容,但是dang图像只是不会复制到uimages文件夹中。请帮忙......
这是代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
function create_thumbnail($source,$destination,$thumb_width){
$size = getimagesize($source);
$width = $size[0];
$height = $size[1];
$x = 0;
$y = 0;
if($width > $height){
$x = ceil(($width - $height) / 2);
$width = $height;
}
else if($height> $width){
$y =(($height - $width) /2);
$height = $width;
}
$new_image = imagecreatetruecolor($thumb_width,$thumb_height) or die('Cannot Initiatlize new GD image stream');
$extension = get_image_extension($source);
if($extension=='jpg' || $extension=='jpeg')
$image= imagecreatefromjpeg($source);
if($extension=='gif')
$image= imagecreatefromgif($source);
if($extension=='png')
$image= imagecreatefrompng($source);
imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
if($extension=='jpg' || $extension=='jpeg')
imagejpeg($new_image,$destination);
if($extension=='gif')
imagegif($new_image,$destination);
if($extension=='png')
imagepng($new_image,$destination);
}
function get_image_extension($name){
$name=strtolower($name);
$i = strrpos($name,".");
if(!$i) {return "";}
$l = strlen($name) - $i;
$extension = substr($name,$i+1,$l);
return $extension;
}
function random_name($length){
$characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$name = "";
for($i = 0; $i < $length; $i++){
$name .= $characters[mt_rand(0,strlen($characters) - 1)];
}
return "image-".$name;
}
$images_location = "images";
$thumbs_location ="uimages/thumbs";
$thumb_width = 100;
$maximum_size = 5120;
$results = "Click to browse to locate the image you want to upload.";
if($_POST){
if($_FILES['image']['name'] == "")
{
$results="No Image Selected. Click Browse to find an image";
}
else{
$size=filesize($_FILES['image']['tmp_name']);
$filename = stripslashes($_FILES['image']['name']);
$extension = get_image_extension($filename);
if($size> $maximum_size){
$results="Your file exceeds the maximum size.";
}
else
if(($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){
$results="Invalid image type. Must be jpg, jpeg, png, or a gif file.";
}
else{
$image_random_name = random_name(15).".".$extension;
$copy= @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name);
if(!$copy){
$results = "Error while uploading image. Please try again.";}
else{
create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name,$thumb_width);
$results="Image has been uploaded";
}
}
}
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5120" >
<input type="file" name="image" />
<input type="submit" value="Upload Image" />
</form>
<?php echo $results; ?>
</body>
</html>
答案 0 :(得分:0)
尝试像这样移动文件:
if (move_uploaded_file($_FILES['image']['tmp_name'], $images_location.$image_random_name))
{
//success
}
else
{
//fail
}
同时检查目标位置是否存在且设置是否正确,以及目标位置的权限是否正确 - 确保apache(或正在运行的任何内容)具有正确的写访问权限。要设置apache的权限,它是这样的:
chown -R nobody directory_name
chmod 755 -R directory_name
答案 1 :(得分:0)
您好我使用以下代码,可以顺利地上传图像。如果您愿意,请检查并使用它。让我知道。
<?php if (isset($_POST['submitted']))
{ // Handle the form.
if (isset($_FILES['image']))
{
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i,$l);
return $ext;
}
function getName($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$name = substr($str,0,$i);
return $name;
}
$image=$_FILES['image']['name'];
//if it is not empty
if ($image)
{
//get the original name of the file from the clients machine
$filename = stripslashes($_FILES['image']['name']);
//get the extension of the file in a lower case format
$extension = getExtension($filename);
$name = getName($filename);
$extension = strtolower($extension);
//if it is not a known extension, we will suppose it is an error and will not upload the file,
//otherwise we will do more tests
if (($extension != ".jpg") && ($extension != ".jpeg") && ($extension != ".png") && ($extension != ".gif"))
{
//print error message
echo '<p class="error">Please upload a JPEG, GIF or PNG image.</p>';
}
else
{
if ($_FILES['image']['error'] > 0)
{
echo '<p class="error">The file could not be uploaded because: <strong>';
// Print a message based upon the error.
switch ($_FILES['image']['error'])
{
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.';
break;
case 3:
print 'The file was only partially uploaded.';
break;
case 4:
print 'No file was uploaded.';
break;
case 6:
print 'No temporary folder was available.';
break;
case 7:
print 'Unable to write to the disk.';
break;
case 8:
print 'File upload stopped.';
break;
default:
print 'A system error occurred.';
break;
} // End of switch.
print '</strong></p>';
} // End of error IF.
else
{
// Query the database:
$q = "SELECT * FROM images WHERE user_id='{$_SESSION["user_id"]}' AND name='$name'";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (@mysqli_num_rows($r) == 0)
{ // A match was made.
$q = "INSERT INTO images (user_id, title, name, extension, profile, date_uploaded) VALUES ('{$_SESSION["user_id"]}', '$name', '$name', '$extension', 'no', NOW() )";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
if (mysqli_affected_rows($dbc) == 1)
{
//we will give an unique name, for example the time in unix time format
$image_name = $name . $extension;
//the new name will be containing the full path where will be stored (images folder)
$newname = "../uploads/".$_SESSION["user_id"]."/".$image_name;
//we verify if the image has been uploaded, and print error instead
move_uploaded_file($_FILES['image']['tmp_name'], $newname);
// Delete the file if it still exists:
if (file_exists ($_FILES['image']['tmp_name']) && is_file($_FILES['image']['tmp_name']) )
{
unlink ($_FILES['image']['tmp_name']);
}
}
else
{
echo '<p class="error">aaa You could not upload pictures due to a system error. We apologize for any inconvenience.</p>';
}
}
else
{
echo '<p class="error">This picture is already uloaded. Choose another.</p>';
}
}
}
}
}
}
?>
<h2>Upload More Pics of You</h2>
<form id="upload_image" name="upload_image" method="post" action="upload_image.php" enctype="multipart/form-data" >
<table width="318" border="0" cellpadding="0" align="left">
<tr>
<td colspan="2" align="center">Select a JPEG, GIF or PNG image of you <br />(512KB or smaller) to be uploaded:</td>
</tr>
<tr>
<td width="47" align="right">Image:</td>
<td width="265">
<input type="file" name="image" id="image" /><input type="hidden" name="MAX_FILE_SIZE" value="524288"> </td>
</tr>
<tr>
<td align="center" colspan="2"><span class="art-button-wrapper">
<span class="l"> </span>
<span class="r"> </span>
<input class="art-button" type="submit" name="submit" value="Submit" /><input type="hidden" name="submitted" value="TRUE" />
</span></td>
</tr>
</table>
</form>