PHP,替换上传的图片

时间:2014-02-01 08:34:24

标签: php mysql

我正在使用以下代码将图像上传到文件服务器,我有一个问题

  1. 如果用户决定因任何原因更改照片,如何将图像替换为另一个图像。
  2. upload_script.php文件 此脚本根据userID和applicationID以及文件位置写入上载文件的详细信息以供以后使用。

    <?php
    session_start();
    
    // insert photo script
    if(!is_dir("upload/".$_SESSION['userName'])) {
        //do we need to make the uploads directory for the files?
        mkdir("upload/".$_SESSION['userName']);
        //make the rest of the script safe, though this will only be done once
    }
    //function time! 
    function savedata(){
        global $_FILES, $_POST, $putItAt;
        $sql = "INSERT INTO `forms`.`files` 
            ( 
            `Time`, 
            `FileLocation`, 
            `IP`, 
            `idUsers`,
            `applicationID`) 
    VALUES (NOW(),'".mysql_real_escape_string($putItAt)."', '". $_SERVER['REMOTE_ADDR']."', '". $_SESSION['userID']."'
    ,'" .$_SESSION['applicationID']."');";
    mysql_query($sql);
    }
    //time to see if the file is uploaded.
    $putItAt = "upload/".$_SESSION['userName']."/" .sha1(rand())."-" .basename($_FILES['uploadedfile']['name']);
    //will they try uploading a script or a page that might be a security risk? 
    
    //lets prevent any .php from getting in. and rename with .txt
    $putItAt = str_replace("php", "txt" , $putItAt);
    
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$putItAt)) {
        //we could echo. but why don't we just go to the file list now? 
        savedata();
        header("location: listfiles");//redirect them to the listfiles page
    
    } else {
        // we failed. Lets try a slightly different method here. instead of moving, try copying
        if(copy($_FILES['uploadedfile']['tmp_name'],$putItAt)) {
            //we have success!
            savedata();
            header("location: listfiles");
    
        } else {
            //we totally failed.. so lets tell them.
            echo 'Image upload has not been successful. click <a href="index.php">here</a> to go back and try again.';
        }
    }
     ?>
    

    表格

    <form id="pro_form1" enctype="multipart/form-data" action="upload_script.php" method="POST">
                    <input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
                    Choose a photo to upload: <input name="uploadedfile" type="file" /><br />
                    <input type="submit" value="Upload File" />
                    <input name="idUsers" type="hidden" value="<?php echo $_SESSION['userID']; ?>">
                    <input name="applicationID" type="hidden" value="<?php echo $_SESSION['applicationID']; ?>">
                </form>
    

    任何帮助都将受到高度赞赏

0 个答案:

没有答案