2015-07-03 168 views
0

使用此代碼一段時間後,我發現人們正在使用空格和逗號上傳文件。從上傳的文件名中刪除空格和逗號

我試過reg_replace這個函數,而且上傳的文件名實際上是用空格替換成下劃線的。

我還需要告訴用戶最終的文件名,所以如果上傳成功,我需要在文本字段中顯示文件名。最後一部分是缺少的。

這怎麼可能在以下情況下完成?

<?php 

$target_dir = "extra_images/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 


// Check if file already exists 
if (file_exists($target_file)) { 
    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>"; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 3750000) { 
    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>"; 
    $uploadOk = 0; 
} 

//Check for pdf format 
if (!empty($_FILES['fileToUpload']['tmp_name'])) { 
    $finfo = finfo_open(FILEINFO_MIME_TYPE); 
    $mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']); 
    if (($mime != 'application/pdf') && ($mime != 'image/jpg') && ($mime != 'image/jpeg') && ($mime != 'image/gif') && ($mime != 'image/png')) { 

     $uploadOk = 0; 
     echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>This file is not a valid file.</strong></div>"; 

     //exit(); 

    }} //this bracket was missing I think 


// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>"; 
// if everything is ok, try to upload file 
} else { 

    $target_file = preg_replace('/\s+/', '_', $target_file);//to replace spaces... 

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 

     echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename($_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\"". basename($_FILES["fileToUpload"]["name"]). "\" class=\"form-control input-sm\" style=\"width:220px;\" /></span> And paste it in an empty Extra image field above and save the form."; 
    } else { 
     echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>"; 
    } 
} 
echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>"; 
exit(); 

?> 
+0

imo,在原地記錄原始文件名,時間戳,用戶標識和'清理過的文件名'。數據庫沒問題。重命名文件以包含標準格式的「清理過的細節」,以便您可以輕鬆找到原始細節。 –

+0

謝謝你的想法,但我知道很少關於PHP或任何面向對象的語言,我學到了pascal和一點C ... –

回答

0

您可以使用str_replace()將空格更改爲下劃線。

$str = 'hai welcome'; 
$newstr = str_replace(' ', '_', $str); 
echo $newstr; 

現在你得到的輸出沒有空間;

+0

謝謝你的答案,但我的意思是在我提供的代碼中已經有一個函數替換空間......'$ target_file = preg_replace('/ \ s + /','_',$ target_file); //替換空格這個函數已經用下劃線保存了名字,但是我也需要在裏面顯示這個文件名如果上傳成功,echo中的文本字段。 –

0

那麼,我已經設法使它工作,它現在將文件名保存到數據庫,這是我的其他需求之一。用戶ip捕獲也可以,但只能使用$ _SERVER ['REMOTE_ADDR'];變量我想知道如何使用get_client_ip()。試圖將$ ipaddress保存到數據庫字段,但沒有奏效...謝謝你的想法和幫助。

<?php 

       $target_dir = "images_folder/"; 
       $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
       $target_file = preg_replace('/\s+/', '_', $target_file);//to replace spaces... 
       $uploadOk = 1; 
       $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

       // Function to get the client IP address 
       function get_client_ip() { 
        $ipaddress = ''; 
        if ($_SERVER['HTTP_CLIENT_IP']) 
         $ipaddress = $_SERVER['HTTP_CLIENT_IP']; 
        else if($_SERVER['HTTP_X_FORWARDED_FOR']) 
         $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; 
        else if($_SERVER['HTTP_X_FORWARDED']) 
         $ipaddress = $_SERVER['HTTP_X_FORWARDED']; 
        else if($_SERVER['HTTP_FORWARDED_FOR']) 
         $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; 
        else if($_SERVER['HTTP_FORWARDED']) 
         $ipaddress = $_SERVER['HTTP_FORWARDED']; 
        else if($_SERVER['REMOTE_ADDR']) 
         $ipaddress = $_SERVER['REMOTE_ADDR']; 
        else 
         $ipaddress = 'UNKNOWN'; 
        return $ipaddress; 
       } 
       $ipaddress2=$_SERVER['REMOTE_ADDR']; 

       // Check if file already exists 
       if (file_exists($target_file)) { 
        echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>"; 
        $uploadOk = 0; 
       } 
       // Check file size 
       if ($_FILES["fileToUpload"]["size"] > 3750000) { 
        echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>"; 
        $uploadOk = 0; 
       } 

       //Check for pdf format 
       if (!empty($_FILES['fileToUpload']['tmp_name'])) { 
        $finfo = finfo_open(FILEINFO_MIME_TYPE); 
        $mime = finfo_file($finfo, $_FILES['fileToUpload']['tmp_name']); 
        if (($mime != 'application/pdf') && ($mime != 'image/jpg') && ($mime != 'image/jpeg') && ($mime != 'image/gif') && ($mime != 'image/png')) { 

         $uploadOk = 0; 
         echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>This file is not a valid file.</strong></div>"; 

         //exit(); 

        }} //this bracket was missing I think 


       // Check if $uploadOk is set to 0 by an error 
       if ($uploadOk == 0) { 
        echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>"; 
       // if everything is ok, try to upload file 
       } else { 



        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 

         $cleanFilename=basename("$target_file"); 

         // Connects to your Database 
         mysql_connect("localhost", "db_username", "db_pass") or die(mysql_error()) ; 
         mysql_select_db("db_name") or die(mysql_error()) ; 

         //Writes the information to the database 
         mysql_query("INSERT INTO tableName (filename,ipaddress) 
         VALUES ('$cleanFilename', '$ipaddress2')") ; 


         echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename($_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\" $cleanFilename \" class=\"form-control input-sm\" style=\"width:320px;\" /></span> And paste it in an empty Extra image field above and save the form. If you see the image after saving you've done right. $cleanFilename"; 
        } else { 
         echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>"; 
        } 
       } 
       echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>"; 
exit(); 
?>