我目前正为我的教师开发学生数据库系统。我正在使用PHP和MySQL。我正在考虑为学生创建一个上传他们的个人资料照片的选项,但我找不到任何正确的指导或教程。
以下是处理文件上传的代码:
<?php
/* Script name: uploadFile.php
* Description: Uploads a file via HTTP with a POST form.
*/
if(!isset($_POST[‘Upload’]))
{
include(“form_upload.inc”);
}
else
{
if($_FILES[‘pix’][‘tmp_name’] == “none”)
{
echo “<p style=’font-weight: bold’>
File did not successfully upload. Check the
file size. File must be less than 500K.</p>”;
include(“form_upload.inc”);
exit();
}
if(!ereg(“image”,$_FILES[‘pix’][‘type’]))
{
echo “<p style=’font-weight: bold’>
File is not a picture. Please try another
file.</p>”;
include(“form_upload.inc”);
exit();
}
else
{
$destination=’c:\data’.”\\”.$_FILES[‘pix’][‘name’];
$temp_file = $_FILES[‘pix’][‘tmp_name’];
move_uploaded_file($temp_file,$destination);
echo “<p style=’font-weight: bold’>
The file has successfully uploaded:
{$_FILES[‘pix’][‘name’]}
({$_FILES[‘pix’][‘size’]})</p>”;
}
}
?>
文件上传表单的代码:
<!-- Program Name: form_upload.inc
Description: Displays a form to upload a file -->
<html>
<head><title>File Upload</title></head>
<body>
<ol><li>Enter the file name of the product picture you
want to upload or use the browse button
to navigate to the picture file.</li>
<li>When the path to the picture file shows in the
text field, click the Upload Picture
button.</li>
</ol>
<div align=”center”><hr />
<form enctype=”multipart/form-data”
action=”uploadFile.php” method=”POST”>
<input type=”hidden” name=”MAX_FILE_SIZE”
value=”500000” />
<input type=”file” name=”pix” size=”60” />
<p><input type=”submit” name=”Upload”
value=”Upload Picture” />
</form>
</div></body></html>
我得到了相同的结果,我无法找到正在上传的文件,并且它没有按原样上传到该位置。
答案 0 :(得分:0)
您应该更改目的地:
$destination=’c:\data’.”\”.$_FILES[‘pix’][‘name’];
或者,如果这不起作用,请尝试将上传的文件移动到脚本路径附近的某处,例如:
$destination= dirname(__FILE__).DIRECTORY_SEPARATOR.$_FILES[‘pix’][‘name’];
如果第二个有效,那么就是手段,你为上传提供了一个错误的目录。
答案 1 :(得分:0)
我认为你应该先改变你的形象路径。 而且你的数据类型手动设置为mysql中的图像, 它会完成。
答案 2 :(得分:0)
你有任何错误消息吗? PHP用户是否具有上载目录的写权限?
此外,这里有两个可能与您的问题无关的建议:
$_FILES[‘pix’][‘tmp_name’]
会成为字符串“none”?