2009-03-31 12 views
6

फ़ाइल अपलोड करने से पहले या बाद में मैं फ़ाइल का नाम कैसे बदलूं? मैं बस फ़ाइल नाम का नाम बदलना चाहता हूं, एक्सटेंशन नहीं।PHP के साथ अपलोड करने के बाद मैं फ़ाइल नाम का नाम कैसे बदलूं?

$changeTXT = $_SESSION['username']; 
$uploaderName = strtolower($changeTXT); 
$changeTXT = strtolower($changeTXT); 
$changeTXT = ucfirst($changeTXT); 
$filelocation = $_POST['userfile']; 
$filename = $_POST['filename']; 
$max_size = $_POST['MAX_FILE_SIZE']; 

$file = $_FILES['userfile']; 

$allowedExtensions = array("wma", "mp3", "wav"); 

function isAllowedExtension($fileName) { 
    global $allowedExtensions; 

    return in_array(end(explode(".", $fileName)), $allowedExtensions); 
} 

if($file['error'] == UPLOAD_ERR_OK) { 
    if(isAllowedExtension($file['name'])) { 

$uploaddir = "uploads/".$uploaderName."/"; 

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 

    echo "Thank you for uploading your music!<br /><br />"; 

} else { 

    echo "Your file did not upload.<br /><br />"; 

} 

    echo "\n"; 

    echo "<a href='index.php'>Return</a> to index.<br /><br />$uploaddir"; 

} else { 

    echo "You have tried to upload an invalid file type.<br /><br />"; 

    } 

} else die("Cannot upload"); 
+0

अच्छा सवाल! Upvoted !! – MJQ

उत्तर

22

जब move_uploaded_file आप फ़ाइल नाम लेने के लिए मिल का उपयोग करते हुए, तो आप कुछ भी आप चाहते हैं चुन सकते हैं।

जब आप फ़ाइल अपलोड करते हैं, तो इसे अस्थायी नाम के साथ अस्थायी निर्देशिका में डाल दिया जाता है, move_uploaded_file() आपको उस फ़ाइल को स्थानांतरित करने की अनुमति देता है और उसमें आपको फ़ाइल का नाम भी सेट करने की आवश्यकता होती है।

+0

महान जवाब! निश्चित रूप से एक अपवित्र की जरूरत है! – MJQ

+1

कोई 'कोड' उदाहरण? – yaqoob

1

जब आप ऐसा करेंगे

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']); 

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { 

वांछित फ़ाइल नाम के पूरे पथ के लिए बस सेट $ uploadfile; यानी "अपलोड/mynewfilename.jpg"

3

क्यों नहीं बस एक अलग नाम के साथ सहेजें:

बस $ uploadfile वैरिएबल का नाम बदलने के लिए:

$uploadfile = $uploaddir . "somefilename" . end(explode(".", $file['name'])); 

यह तथापि यदि चोट नहीं होगा आप अपने कोड को थोड़ा सा प्रतिक्रिया देते हैं।

6

मैं आपको Verot की फ़ाइल अपलोड क्लास देखने के लिए सुझाव दूंगा। यह php के माध्यम से फ़ाइल अपलोड करने के बहुत सारे दर्द को ठीक करता है, और आपके कोड को और अधिक पठनीय/रखरखाव बनाता है।

यहां link to the class और दस्तावेज़ीकरण है।

आपके प्रश्न के सटीक उत्तर के रूप में: अपलोड की गई फ़ाइल में नया नाम देने के लिए, इसे अपने move_uploaded_file() फ़ंक्शन पर दूसरा तर्क दें।

आप, विस्तार रखने के एक चर में पहली यह संग्रहीत करना चाहते हैं के बाद से:

$ext = explode('.',$_FILES['uploaded']['name']); 
$extension = $ext[1]; 

आप (एक टाइमस्टैम्प इसके परिशिष्ट के साथ, यहाँ) फ़ाइल रूट नाम इस्तेमाल कर सकते हैं नया नाम उत्पन्न करने के लिए:

$newname = $ext[0].'_'.time(); 

कहें कि आपने एक फ़ाइल अपलोड की है, और आपका फॉर्म इनपुट वैरिएबल नाम "myfile.png" है, यह 'myfile_2343544.png' बन जाएगा;

$full_local_path = 'path/to/your/filefolder/'.$newname.'.'.$extension ; 
move_uploaded_file($_FILES['uploaded']['tmp_name'], $full_local_path); 
+1

ऑलस्टोस्ट। लेकिन आपको $ newname की आवश्यकता है। "$ एक्सटेंशन ", नए नाम और एक्सटेंशन के बीच की अवधि जोड़ना, अन्यथा यह" filenamejpg "आता है। उपरोक्त! – Cyprus106

+1

मैंने डॉट्स के बीच की जगह में खुद को खो दिया ... अच्छा पकड़, धन्यवाद !! – pixeline

1

आप move_uploaded_file में दूसरा पैरामीटर का उपयोग कर सकते हैं फ़ाइल नाम सेट करने के लिए:

अब, समारोह का दूसरा तर्क स्थापित करने के लिए लक्ष्य निर्देशिका, $ NewName वर और फ़ाइल का विस्तार करने के लिए स्थानीय पथ गठबंधन। पहला पैरा आपके/tmp फ़ोल्डर में फ़ाइल का फ़ाइल नाम है और दूसरा पैरा फ़ाइल डीआईआर और नया फ़ाइल नाम है।

move_uploaded_file($_FILES['file']['tmp_name'], "/new/file/dir/".[the new file name]); 

मुझे पता है कि यह एक पुराना सवाल है लेकिन स्पष्ट उत्तर नहीं था।

0
<?php 

    /* 
    Just change the $path variable to your uploads path 
    and dont forget the/at the end of the path. 

    The way this function works is .When the user submits the form 
    the uniqueFilename() function checks to see if there is a filename 
    in your uploads folder, that has the same name as the one you intend 
    to upload. If it finds a filename that has the same name as the one you want to 
    upload it goes through the next steps 


     Step 1 

    strrpos($arg1,$arg2) - we use it to find the location where upload path ends and where the filename begins 

    $arg1 = the filepath including the filename ex. uploads/myFile.jpg 
    $arg2 = basename($filename); what this does is it strips the path from the string and returns only the filename. 

    When we use strrpos($arg1,$arg2) we basically tell the function to find the position of the path and exclude the filename 

    Step 2 

    substr($arg1,$arg2,$arg3) - looks in a string and extrats only the parts from the string that you want. We use it now to extract the uploads path. 

    $arg1 = the full path of the uploaded file. ex. uploads/myFile.jpg 
    $arg2 = 0 because we want to start from the begginning of the string 
    $arg3 = $pathlocation which is the location of where the uploadpath ends and the file beggins 

    when we use substr() here we intend to extract the uploads path. 

    Step 3 

    basename($arg1) strips out a path and returns the filename 

    - we use this to save the original filename 

    Notice that so far we have the path to the files saved and also 
    the filename with its extension. We also have the file extension (.jpg|.png) or whatever 


    Step 4 

    pathinfo() - extracts the extension from the filename 

    Step 5 

    we us strrpos() to find the location of the last dot (.) in the filename because in step 6 
    we use this location to cut the string where the dot (.) is 

    Step 6 

    substr() gets the file without the extension 

    Step 7 
    substr() takes the filename without extension and clips out the last charcter 
    so if the filename is myFile this function would return myFil 

    Step 8 
    substr() again we use it to save the last character of the file so 
       if our file is called myFile it would return e. 

    Step 9 
    Here we check to see if the last character of our file is numeric. 
    Why do this? Because if the last character of a file is numeric we can just add a number to the 
    file and end up with a new file with an added number so our file is 
    myFile1 the 1 is numeric so our intention is to add to it 
    so my myFile1 would end up being myFile2. But what happens if our last character is not numeric? 

    then we just add -1 to the file so that myFile would end up being myFile-1 

    Step 10 
    Since our filename exists in the database we must call the function uniqueFilename() again so that we can 
    check and see if the new name exists in the database. 

    This function basically keeps calling itself until it finds a filename that does not exists. 
    */ 





    if(isset($_POST['submit'])){ 

    $path = "uploads/"; 

    $fullPath = $path.$_FILES['uploadFile']['name']; 
    $full_local_path = $path.uniqueFilename($fullPath) ; 
    move_uploaded_file($_FILES['uploadFile']['tmp_name'], $full_local_path); 
    } 
      /* 
      * Function returns a unique filename 
      * Param 1 is the path of the filename and the filename 
      * returns filename with no path 
      */ 
      function uniqueFilename($filepathAndFilename){ 

       // check to see if file exists 
       if (file_exists($filepathAndFilename)) 
       { 
         /* step 1 */ 

        //Get the path location without the basename 
        $filePathWithoutFilenameLocationInString = strrpos($filepathAndFilename,basename($filepathAndFilename)); 

         /* step 2 */ 

        //Get the path and cut the basename 
        $path = substr($filepathAndFilename,0,$filePathWithoutFilenameLocationInString); 

         /* Step 3*/ 
        //Get the base filename 
        $filename = basename($filepathAndFilename); 

         /* Step 4 */ 
         // get the extension 
        $fileExtension = pathinfo($filename, PATHINFO_EXTENSION); 

         /* Step 5 */ 
        // find the position of the last dot 
        $dotLocation = strrpos($filename,'.'); 

         /* Step 6 */ 
        // Get the filename without the dot 
        $fileWithNoExtension = substr($filename,0,$dotLocation); 

         /* Step 7 */ 
        //Get the name without the last character 
        $filenameMinusLastCharacter = substr($fileWithNoExtension,0,-1); 

         /* Step 8 */ 
        //save the last character 
        $lastFilenameCharacter = substr($fileWithNoExtension,-1); 

         /* Step 9*/ 
        //if the last character is numeric add 1 
        if (is_numeric($lastFilenameCharacter)) 
        { 
         $newFilename = $path.$filenameMinusLastCharacter.($lastFilenameCharacter+1).'.'.$fileExtension; 
         //recursively call itself 

         /* Step 10*/ 
         $results = uniqueFilename($newFilename); 
         return basename($results); 
        }else{ 

         // if the last character is not numeric then add 1 
         $newFilename = $path.$fileWithNoExtension.'-1'.'.'.$fileExtension; 

          /* Step 10*/ 
         //recursively call itself 
         $results = uniqueFilename($newFilename); 
         return basename($results); 
        } 

       } 
       return basename($filepathAndFilename); 

      } 
    ?> 

    <!doctype html> 
    <html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Untitled Document</title> 
    </head> 

    <body> 

    <form method="post" enctype="multipart/form-data"/> 
    <label>Upload File</label> 
    <input type="file" name="uploadFile"/> 
    <input type="submit" name="submit" value="submit"/> 
    </form> 

    </body> 
    </html>`enter code here` 
+0

यदि आप यह भी समझाते हैं तो यह हमेशा सहायक होता है जवाब। यह आपके उत्तर को और भी बेहतर बनाएगा, और ऊपर उठने की संभावना अधिक होगी। – Magnilex

संबंधित मुद्दे