为学生数据库实现个人资料照片上传

发布于 2025-01-02 14:28:47 字数 2197 浏览 2 评论 0原文

我目前正在为我的教师开发一个学生数据库系统。我将 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>

我得到了相同的结果,我找不到正在上传的文件,并且它没有上传到应有的位置。

I am currently develop a student database system for my faculty. I am using the PHP together with MySQL. I am thinking to create an option for student to upload their profile photo but I could not find any proper instruction or tutorial of doing that.

Here's the code processing the file uploading:

<?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>”; 
    }
  }
?>

Code for the file upload form:

<!-- 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>

I got the same outcome which I cant find the file that being uploaded and it is not being uploaded to the location as it should be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天旳寂寞 2025-01-09 14:28:47

您应该更改目的地:

$destination=’c:\data’.”\”.$_FILES[‘pix’][‘name’];

或者,如果这不起作用,请尝试将上传的文件移动到脚本路径附近的某个位置,例如:

$destination= dirname(__FILE__).DIRECTORY_SEPARATOR.$_FILES[‘pix’][‘name’];

如果第二个有效,则意味着您为上传提供了错误的目录。

You should change the destination:

$destination=’c:\data’.”\”.$_FILES[‘pix’][‘name’];

Or, if this is not working, try to move the uploaded file somewhere near the script path, like:

$destination= dirname(__FILE__).DIRECTORY_SEPARATOR.$_FILES[‘pix’][‘name’];

If the second one works then is means, you have given a wrong directory for the upload.

回忆追雨的时光 2025-01-09 14:28:47

我认为你应该首先改变你的图像路径。
比你的数据类型手动设置为mysql中的图像,
它将完成。

I think you should first change your image path.
and than your datatype set to image in mysql manually,
it will be done.

沫雨熙 2025-01-09 14:28:47

您收到任何错误消息吗? PHP用户对上传目录有写权限吗?

另外,这里有两个可能与您的问题无关的建议:

  • 不要在任何新代码中使用 eregi。它已被弃用,这意味着它将在 PHP 的未来版本中被删除。相反,请使用 preg_ 函数,或者在本例中仅使用 strpos。
  • 为什么 $_FILES['pix']['tmp_name'] 会是字符串“none”?

Do you get any error messsage? Do the PHP user have write permissions to the upload directory?

Also, here's two advices possibly not related to your question:

  • Don't use eregi in any new code. It is deprecated, which means it will be removed in a future version of PHP. Instead, use the preg_ functions, or just strpos in this case.
  • Why would $_FILES[‘pix’][‘tmp_name’] ever be the string "none"?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文