如何上传照片到我的主机服务器文件夹目录


How to upload photo to my hosting server folder directory

你好,我正试图使用php将我制作的网站上的照片上传到网站托管服务器的文件夹目录(/public_html/upload/)。但是文件夹总是显示为空。我不知道我做错了什么。有人能帮忙吗?

这是html文件:

 <div id = "forms">
  <form action="add_data.php" method="post" form enctype="multipart/form-data">
   <div id = "info1">
   <p> <h2> Accident Report Form </h2> </p>
   </div>
    <div id = "info2"><p> Please fill in the form below to report an accident </p></div>
      <ol>
       <li> <label for = "name" >  Name: </label> 
          <input "type = "text" name = "name" id = "name"/> </li>
         <li> <label for = "location" > Location of the accident: </label>
          <input "type = "text" name = "location" id = "location" /> </li>
         <li> <label for = "road" > Road Name: </label>
          <input "type = "text" name = "road" id = "road"/> </li>

                 <li> <label for = "image" > Image: </label>
          <input type="file" name="photo"><br> </li>

      </ol>
     <div id = "submit"><button type="submit" >Submit</button> </div><br>    

  </form>

 </div>

这是add_data.php文件:

 <?php
$target = "/public_html/upload"; 
 $target = $target . basename( $_FILES['photo']['name']);
 $pic=($_FILES['photo']['name']);
   ?> 

强制工作代码:

<?php
   $target = "/public_html/upload/"; 
   $target = $target .$_FILES['photo']['name'];
   $pic=($_FILES['photo']['tmp_name']);
   move_uploaded_file($pic, $target);
?> 

我已经更新了您的代码以使其正常工作。

HTML-可以,但可以更好地格式化,请尝试在线格式化工具http://jsbeautifier.org/.
此外,表单打开标签应该看起来像这个

<form action="add_data.php" method="post" enctype="multipart/form-data">

PHP-注释中提供的解释

<?php
// Uploaded file will be stored in a temporary location and needs to
// be moved to a destination directory and given the original filename
//-----------------------------------------------------------------
//  prepare target pathname
//
$target = "/public_html/upload";
$targetName = $_FILES['photo']['name'];
// get temp file name
//
$tmp = $_FILES['photo']['tmp_name'];
// use php's move_uploaded_file() function
// to copy temp file to final destination
move_uploaded_file($tmp, $targetDir . $targetName);