如何上传照片到我的托管服务器文件夹目录 [英] How to upload photo to my hosting server folder directory

查看:173
本文介绍了如何上传照片到我的托管服务器文件夹目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是html文件:

 << ; div id =forms> 
< form action =add_data.phpmethod =postform enctype =multipart / form-data>

< div id =info1>
< p> < H2>事故报告表格< / h2> < / p为H.
< / div>
< div id =info2>< p>请填写以下表格以举报意外< / p>< / div>



    < li> < label for =name>名称:< / label>

    < input" type =textname =nameid =name/>< / li>

    < li>< label事故发生地点:< / label>

    < inputtype =textname =locationid =location/> < /锂>

    < li> < label for =road>道路名称​​:< / label>

    < input" type =textname =roadid =road/>< / li>


    < li> ;< label for =image>图片:< / label>


    $ b< / ol>
    < div id =submit>< button type =submit>提交< / button>< / div> ;< br>


    < / form>


    < / div>

这是 add_data.php 文件:

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


解决方案

我已经更新了你的代码。格式化得更好,试试一个在线格式化工具
http://jsbeautifier.org/
此外,表格的开标签应该像这样

 < form action =add_data.phpmethod =postenctype =multipart / form-data> 

PHP - 评论中提供的解释

 <?php 

//上传的文件将存储在临时位置,需要
// be移动到目标目录并给出原始文件名
// ---------------------------------- -------------------------------

//准备目标路径名
//
$ target =/ public_html / upload;
$ targetName = $ _FILES ['photo'] ['name'];

//获取临时文件名
//
$ tmp = $ _FILES ['photo'] ['tmp_name'];
$ b $ //使用php的move_uploaded_file()函数
//将临时文件复制到最终目的地
move_uploaded_file($ tmp,$ targetDir。$ targetName);


Hello I was trying to upload a photo from a website I made, to the website's hosting server's folder directory (/public_html/upload/) using php. but the folder always shows up empty. I dont know what am I doing wrong. can anyone help?

this is the html file:

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

And, this is the add_data.php file:

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

解决方案

I have updated your code to work.

HTML - is ok but could be formatted better, try a online formatter tool http://jsbeautifier.org/.
Also, form opening tag should look like this

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

PHP - Explanation provided in the comments

<?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);

这篇关于如何上传照片到我的托管服务器文件夹目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆