您需要在表单中添加:
<input type="file" name="imagefile">
enctype="multipart/form-data"
在你
的
IE:
<form action="../login/comment-exec.php" method="POST" enctype="multipart/form-data">
然后在您的表单处理程序中的某个地方:
$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation.
$max_filesize = 9999999999; // Maximum filesize in BYTES - SET IN to a low number for small files
$upload_path = './uploads/'; // The place the files will be uploaded to (currently a 'files' directory).
$filename = $_FILES['imagefile']['name']; // Get the name of the file (including file extension).
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.
// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
// Now check the filesize, if it is too large then DIE and inform the user.
if(filesize($_FILES['imagefile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
// Check if we can upload to the specified path, if not DIE and inform the user.
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
// Upload the file to your specified path.
if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$upload_path . $filename))
// Echo success and the uploaded file.
echo 'Your file upload was successful, view the file <img src="' . $upload_path . $filename . '" title="Anything you want">'; // It worked.
else
echo 'There was an error during the file upload. Please try again.'; // It failed
// rest of your code to be placed below
DB 注意:为了在数据库中插入上传的文件或对其的引用,图像文件本身可能需要在此过程中重命名,或者为其设置唯一 ID。我在这个领域没有任何专业知识,但我知道您的记录需要是独一无二的。
例如,人们从 iPod Touch 或 iPhone 等上传 JPG。图像的默认名称为image.jpg
. 任何人从这些设备上传图像且未重命名,将自动覆盖之前上传的图像image.jpg
;只是深思。