How to create destination (Folder) in PHP while using move_uploaded_file()?

前端 未结 5 1418
无人共我
无人共我 2020-12-29 06:41

I want to upload files with PHP and i use move_uplload_files to copy them to the destination folder I want, everything works fine with this :

if (move_upload         


        
5条回答
  •  隐瞒了意图╮
    2020-12-29 07:36

    This works for me:

    $path = "upload/";
    $name = $_FILES["file"]["name"];
    // Remove dangerous characters from filename.
    $name = str_replace('..', '', $name);
    $name = str_replace('/', '', $name);
    $name = str_replace('\\', '', $name);
    
    if (($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    && ($_FILES["file"]["size"] < 2000000)) {
          if ($_FILES["file"]["error"] > 0) {
            echo "Error " . $_FILES["file"]["error"] . "
    "; } else { if(file_exists($path.$name)){ echo "$path$name already exists. "; } else { @mkdir($path, 0666, true); // Create non-executable upload folder(s) if needed. move_uploaded_file($_FILES["file"]["tmp_name"], $path.$name); echo "Stored in: $path$name"; } } } else { echo "Invalid file. Allowed are JPG smaller than 2 MB."; }

提交回复
热议问题