首页 > 解决方案 > 如何将上传文件路径设置为不同的url

问题描述

我正在domain.com上上传文件,目前正在将文件保存到domain.com/upload路径。但我想将上传文件路径设置为另一个全局 url 的目录,即 www.domain2.com/uploads目录。因此,我们可以将来自多个域的所有上传文件保存在一个目录中。

这是我的upload.php

    <?php

include('../../../connect.php');

error_reporting(0);
if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {

    $appid = $_POST['appid'];

    $link_address = "../upltp.php?appid=" . $appid . "";

    //$path = "https://www.domain2.com/uploads/"; //set your folder path
    $path = "../../../uploads/"; //set your folder path
    //set the valid file extensions 
    $valid_formats = array("jpg", "png", "gif", "bmp", "jpeg", "GIF", "JPG", "JPEG", "PNG", "doc", "txt", "docx", "pdf", "PDF", "xls", "xlsx"); //add the formats you want to upload

    $name = $_FILES['myfile']['name']; //get the name of the file

    $size = $_FILES['myfile']['size']; //get the size of the file

    if (strlen($name)) { //check if the file is selected or cancelled after pressing the browse button.
        list($txt, $ext) = explode(".", $name); //extract the name and extension of the file
        if (in_array($ext, $valid_formats)) { //if the file is valid go on.
            if ($size < 30098888) { // check if the file size is more than 15 mb
                $file_name = $_POST['filename']; //get the file name
                $tmp = $_FILES['myfile']['tmp_name'];
                $imgpics =  $file_name.'.'.$ext;
                if (move_uploaded_file($tmp, $path . $file_name.'.'.$ext)) { //check if it the file move successfully.                   
                     $query = "INSERT INTO payments (app_id, imgpics) VALUES('$appid', '$imgpics') ON DUPLICATE KEY UPDATE app_id='$appid', imgpics='$imgpics'";
                     mysqli_query($connect,$query);                 
                    echo "File uploaded successfully!!
                    <br><br><a href='".$link_address."' class='butto'>Click here to Proceed to Next Step</a>";
                } else {
                    echo "failed";
                }
            } else {
                echo "File size max 15 MB";
            }
        } else {
            echo "Invalid file format..";
        }
    } else {
        echo "Please select a file..!";
    }

    exit;
}

我们如何设置该 url 的路径,以便它可以将来自任何域的文件保存到路径的 url?

标签: phpfile-upload

解决方案


推荐阅读