dsrjs86444 2018-06-03 19:43
浏览 44
已采纳

PHP上传文件和写TEXT文件

This SHD work ... no idea why. When user submits form, uploads the file to ../images/ and writes that file name to a text file. It does not upload the file and it blanks out the txt file which I assume means the upload fails.

$img_name = $_FILES['avatar']['name'];
$upload_file = move_uploaded_file($_FILES['avatar']['tmp_name'], "../images/" . $img_name);

$log_avatar = fopen('../archive/avatar.txt', 'w+');
fwrite($log_avatar, $img_name);
fclose($log_avatar);

header("location:user.php");
  • 写回答

1条回答 默认 最新

  • dongzhuang6417 2018-06-03 21:01
    关注

    to write the name of the uploaded file to text file use "a+" instead of "w+" the difference between them is

    "w+" opens the file for reading and writing and place the file pointer at the beginning of the file

    "a+" opens the file for reading and writing and place the file pointer at the end of the file

    $log_avatar = fopen('../archive/avatar.txt', 'a+');
    

    also you can add line break using PHP_EOL

    fwrite($log_avatar, $img_name.PHP_EOL);
    

    that is what makes the text file get empty

    about upload files there is nothing wrong in your code. but i think you are have error in your html form because filename is blank

    make sure you set the form enctype Attribute to "multipart/form-data"

    <form method="POST" enctype="multipart/form-data" action="up.php">
    

    and using the correct input name in your upload script

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

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料