doushan2811 2014-10-16 23:15
浏览 74
已采纳

使用PHP和HTML上传文件

I'm having trouble with setting up the file upload to my php script.

Within the upload itself, I have it directed towards the path I want, but I'm not sure if I have the rest of the script syntactically right.

EDIT: I have resolved my situation with the file not properly uploading. I have edited the blocks of code to what I have currently working. If needed, please refer to the pre-edited version to see changes :).

Cheers.

$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
$uploadOk=1;

if(isset($_FILES["uploadFile"])){
    $tardir= $tardir . "/" . basename($_FILES["uploadFile"]["name"]);
    if(move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $tardir)){ 
        echo "The file: ". basename($_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

The $targetnameis set to whatever the user inputs in the field. In this case, I have it as their email.

$targetname = filter_input(INPUT_POST, 'email');

This is what the <html> for the file upload looks like:

<form action= "userlogin.php" method= "post" enctype= "multipart/form-data">
        <p><strong>Please choose a file to upload: <input type="file" name="uploadFile"><br></p>
           <input type="submit" value="Upload File">
  • 写回答

1条回答 默认 最新

  • douan7529 2014-10-17 01:07
    关注

    It's hard to guess all your code, as it seems you only have a snippet here, but I'm gonna take a wild guess. If target name comes from a text input like you say and you have it set to e-mail, are you leaving off a slash here?

    $tardir= "C:/xampp/htdocs/uploaddir/$targetname";
    

    This would be like:

    C:/xampp/htdocs/uploaddir/me@gmail.com
    

    This runs:

    $tardir= $tardir . basename($_FILES["uploadFile"]["name"]);
    

    then:

     C:/xampp/htdocs/uploaddir/me@gmail.comfilename
    

    shouldn't it be:

    C:/xampp/htdocs/uploaddir/me@gmail.com/filename
    

    you can also comment out all your code here and var_dump() your file array, and echo out your paths before you try using them to see what data is held in each of your containers. If I get a bug like this I dump everything and echo every string until I find it..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路