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

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)