将图像复制到新位置 - PHP

时间:2017-08-31 08:00:38

标签: php image file-upload copy rename

我想从临时位置复制并映像到用户的后台文件夹,但是当我尝试它时,它总是将我引导到错误页面。

代码:
在这里,我设置了$fileTmpLoc$fileName变量

$imgtype = $_POST['imgtype'];
    $fileTmpLoc = "";
    $fileName = "";
    if($imgtype == "universe"){
        $fileTmpLoc = "../images/universebi.jpg";
        $fileName = "universebi.jpg";
    }else if($imgtype == "flowers"){
        $fileTmpLoc = "../images/flowersbi.jpg";
        $fileName = "flowersbi.jpg";
    }else if($imgtype == "forest"){
        $fileTmpLoc = "../images/forestbi.jpg";
        $fileName = "forestbi.jpg";
    }else if($imgtype == "bubbles"){
        $fileTmpLoc = "../images/bubblesbi.jpg";
        $fileName = "bubblesbi.jpg";
    }else if($imgtype == "mountains"){
        $fileTmpLoc = "../images/mountainsbi.jpg";
        $fileName = "mountainsbi.jpg";
    }else if($imgtype == "waves"){
        $fileTmpLoc = "../images/wavesbi.jpg";
        $fileName = "wavesbi.jpg";
    }else if($imgtype == "stones"){
        $fileTmpLoc = "../images/stonesbi.jpg";
        $fileName = "stonesbi.jpg";
    }else if($imgtype == "simple"){
        $fileTmpLoc = "../images/simplebi.png";
        $fileName = "simplebi.png";
    }

在这里,我设置$fileExt变量并创建background文件夹(如果尚未创建)。

$kaboom = explode(".", $fileName);
    $fileExt = end($kaboom);

    $db_file_name = "bibg".rand(3333,7777).".".$fileExt;
    if (!file_exists("../user/$log_username/background")) {
        mkdir("../user/$log_username/background", 0755);
    }

这是错误的代码。我想将图片从$fileTmpLoc复制到$newFileLoc,但它失败了。但是,如果我使用rename功能尝试它,它可以正常工作,但我不想删除图像,因为这些图像在我的计算机中。所以,我想以某种方式复制它们并移动到正确的文件夹。

$newFileLoc = "../user/$log_username/background/$db_file_name";
    if(!copy($fileTmpLoc, $newFileLoc)){
        header('location: ../file_upload_error.php');
        exit();
    }

最后,我只是将变量放入数据库中

$sql = "UPDATE useroptions SET background='$db_file_name' WHERE username='$log_username' LIMIT 1";
    $query = mysqli_query($conn, $sql);
    mysqli_close($conn);
    echo "bibg_success";
    exit();

那有什么解决方案吗?

0 个答案:

没有答案
相关问题