doukuang1950 2017-05-08 20:10
浏览 34

我如何使用PHP上传文件的格式

How do I upload files and move them to their perspective folder using php, I'm trying to make the script automatically upload

images such as jpg and png to jpg/ folder mp3 to mp3/ folder and mp4 flv mpeg to mp4 folder

<?php
$ds          = DIRECTORY_SEPARATOR;

$jpg = "jpg/";
$mp3 = "mp3/";
$mp4 = "mp4/";


if (!empty($_FILES))
{
  if(substr($_FILES['file']['name'], -3) == "mp3")
  {
    $destination = substr($_FILES['file']['name'], -3);
    $file        =   $_FILES['file']['name'];

    echo "this : " . $mp3,$destination . $ds;
    move_uploaded_file($mp3,$destination . $ds ) or die("mp3 error");
    echo "uploaded : " . $file . "<br>";
  }
  else if(substr($_FILES['file']['name'], -3) == "mp4" or substr($_FILES['file']['name'], -3) == "mp4" or substr($_FILES['file']['name'], -4) == "mpeg" )
  {
    $destination = substr($_FILES['file']['name'], -3);
    $file        =   $_FILES['file']['name'];

    move_uploaded_file($mp4,$destination . $ds ) or die("mp4 error");
    echo "uploaded : " . $file . "<br>";
  }
  else if(substr($_FILES['file']['name'], -3) == "jpg" or substr($_FILES['file']['name'], -3) == "png")
  {
    $destination = substr($_FILES['file']['name'], -3);
    $file        =   $_FILES['file']['name'];

    move_uploaded_file($jpg,$destination . $ds ) or die("jpg error");
    echo "uploaded : " . $file . "<br>";
  }
  else
  {

  }
}
?>
  • 写回答

1条回答 默认 最新

  • donglang5157 2017-05-08 20:23
    关注

    Read move_uploaded_file manual.

    First argument is "The filename of the uploaded file.". And you pass what:

    move_uploaded_file($mp3,$destination . $ds )
    

    $mp3 is a folder mp3.

    Usually you need to pass $_FILES['file']['tmp_name'] as first argument.

    Second note - concatenation $destination . $ds returns what?

    In case of music.mp3 file it will be mp3/.

    So you call move_uploaded_file with arguments:

    move_uploaded_file('mp3/', 'mp3/');
    

    Do you really want this?

    So, the simpliest fix is (for an mp3 case):

    move_uploaded_file($_FILES['file']['tmp_name'], $mp3 . $_FILES['file']['name']);
    

    In this case file will be saved in mp3 folder on the same level as your script with file's original name.

    Also make sure that mp3 folder exists and has proper permissions.

    评论

报告相同问题?

悬赏问题

  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法