我在fileupload项目上做了很多事情,但是存在使项目停止的问题 当我上传图片时,它不会出现任何问题或任何错误,但上传的图片不会显示在'/ uploads文件夹
我使用的代码
<html>
<head>
<title>image uploader</title>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
image selector :
<input type="file" name="uploadsrc" /> <input type="submit" value="Upload">
</form>
<style type="text/css">
body{;
font-family: sans-serif;
}
<?php
//connected to database
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("meinimages") or die(mysql_error());
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
// basename() may prevent filesystem traversal attacks;
// further validation/sanitation of the filename may be appropriate
$name = basename($_FILES["pictures"]["name"][$key]);
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
?>
<?php
// file properties
@$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
if ($image_size==FALSE)
echo "you try upload nonimage.";
else
{
if (!$insert = mysql_query("INSERT INTO store VALUES ('','$image_name','$image')"))
echo "problem while upload the $image.";
else
{
$lastid = mysql_insert_id();
echo "image is uploaded.<p />your image:<p /><img src=get.php?id=$lastid>";
}
}
}
ps:这是我的代码错误还是数据库/ phpmyadmin上的错误?
答案 0 :(得分:1)
正如我在评论中所建议的那样,你的php代码正在寻找一个名为pictures
的表单元素,但是表单本身有一个名为uploadsrc
的字段,因此对任何其他问题的重新处理它永远不会实际处理上传。该代码还建议上载多个文件,但表单本身没有在文件字段上设置multiple
属性,因此只允许单个文件。下面的代码用于单个文件上传 - 虽然转换为接受多个是相对简单的。希望以下内容有用 - 但您真的应该考虑使用mysqli
,以便您可以使用prepared statements
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("meinimages") or die(mysql_error());
/* utility function to return error message during upload processing */
function uploaderror( $code ){
switch( $code ) {
case UPLOAD_ERR_INI_SIZE: return "The uploaded file exceeds the upload_max_filesize directive in php.ini";
case UPLOAD_ERR_FORM_SIZE: return "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
case UPLOAD_ERR_PARTIAL: return "The uploaded file was only partially uploaded";
case UPLOAD_ERR_NO_FILE: return "No file was uploaded";
case UPLOAD_ERR_NO_TMP_DIR: return "Missing a temporary folder";
case UPLOAD_ERR_CANT_WRITE: return "Failed to write file to disk";
case UPLOAD_ERR_EXTENSION: return "File upload stopped by extension";
default: return "Unknown upload error";
}
}
/* The form file field name */
$field='uploadsrc';
?>
<html>
<head>
<title>image uploader</title>
<style type="text/css">
body{font-family: sans-serif;}
</style>
</head>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
image selector :<input type="file" name="uploadsrc" />
<input type="submit" value="Upload">
</form>
<?php
/* Only process for POST requests */
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_FILES[ $field ] ) ){
/* Restrict file extensions possibly */
$permitted=array('jpg','jpeg','png','gif');
/* Define a maximum allowed file size perhaps */
$maxfilesize=pow(1024,2) * 2.5; /* 2.5Mb */
/* placeholder array to receive errors during processing */
$errors=array();
/* target location to save images */
$uploads_dir = '/uploads';
/* Get the upload properties */
$obj=(object)$_FILES[ $field ];
$name=$obj->name;
$tmp=$obj->tmp_name;
$size=$obj->size;
$error=$obj->error;
$type=$obj->type;
if( $error == UPLOAD_ERR_OK && is_uploaded_file( $tmp ) ){
/* define the full path to save the image to */
$targetfile = $uploads_dir . DIRECTORY_SEPARATOR . $name;
/* Get properties of the uploaded image */
$ext=pathinfo( $name, PATHINFO_EXTENSION );
$imgsize=getimagesize( $tmp );
/* rudimentary error checks */
if( !in_array( $ext, $permitted ) ) $errors[]='File type is not allowed';
if( !$imgsize ) $errors[]='Not an image';
if( $size > $maxfilesize ) $errors[]='Image is too large';
/* Attempt to move the file to it's final location */
$status = empty( $errors ) ? move_uploaded_file( $tmp, $targetfile ) : false;
if( $status ){
/*
Generally it is not considered a good idea to store the actual file contents
in the database due to the size implications. The db will likely grow very large
and become more unstable potentially. Better to simply store the path to the uploaded
file!
*/
$data = addslashes( file_get_contents( $targetfile ) );
$sql="insert into `store` values ('','$name','$data');";
$status = mysql_query( $sql );
$lastid = $status ? mysql_insert_id() : false;
echo $status ? "problem while uploading image" : "image is uploaded. <p>Your image:</p><img src=get.php?id=$lastid>";
} else {
/* Display any error messages */
foreach( $errors as $err ){
echo $err . '<br />';
}
}
} else {
if( !$error == UPLOAD_ERR_OK ){
exit( uploaderror( $error ) );
}
}
}
?>