2011-11-09 23 views
6

Sto creando un'applicazione che consente agli utenti di caricare un file in una directory tramite PHP.PHP Caricamento file e sovrascrittura file con lo stesso nome

Ho problemi perché la dose non mi consente di sovrascrivere i file con lo stesso nome. Ad esempio, ho un file chiamato text.php e lo carico, ora quando torno indietro e cambio il contenuto del file text.php e lo carico di nuovo sul server ho ancora la versione senza le modifiche. Tuttavia se carico un altro file funziona. Quindi non riesco a sovrascrivere i file.

if ($_POST["greg"]=='true'){ 
// Set local PHP vars from the POST vars sent from our form using the array 
// of data that the $_FILES global variable contains for this uploaded file 
$fileName = $_FILES["file1"]["name"]; // The file name 
$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder 
$fileType = $_FILES["file1"]["type"]; // The type of file it is 
$fileSize = $_FILES["file1"]["size"]; // File size in bytes 
$fileErrorMsg = $_FILES["file1"]["error"]; // 0 for false... and 1 for true 

// Specific Error Handling if you need to run error checking 
if (!$fileTmpLoc) { // if file not chosen 
    echo "ERROR: Please browse for a file before clicking the upload button."; 
    exit(); 
} else if($fileSize > 90000000000000) { // if file is larger than we want to allow 
    echo "ERROR: Your file was larger than 50kb in file size."; 
    unlink($fileTmpLoc); 
    exit(); 
} else if (!preg_match("/.(doc|docx|xls)$/i", $fileName)) { 
    // This condition is only if you wish to allow uploading of specific file types  
    echo "ERROR: Your file is not the right format contact the master of the page for clarification."; 
    unlink($fileTmpLoc); 
    exit(); 
} 
// Place it into your "uploads" folder mow using the move_uploaded_file() function 
move_uploaded_file($fileTmpLoc, "documenti/$fileName"); 
// Check to make sure the uploaded file is in place where you want it 
if (!file_exists("documenti/$fileName")) { 
    echo "ERROR: File not uploaded<br /><br />"; 
    echo "Check folder permissions on the target uploads folder is 0755 or looser.<br /><br />"; 
    echo "Check that your php.ini settings are set to allow over 2 MB files, they are 2MB by default."; 
    exit(); 
} 
// Display things to the page so you can see what is happening for testing purposes 
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />"; 
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />"; 
echo "It is a <strong>$fileType</strong> type of file.<br /><br />"; 
echo "The Error Message output for this upload is: <br />$fileErrorMsg"; 

} 

Come posso modificare il codice in modo che quando aggiungo un file con lo stesso nome sovrascrive il file esistente?

+0

Sei sicuro che non ottiene sovrascritto, con il codice standard comportamento su un server simile a LAMP è che il file verrà sovrascritto. Quindi assicurati che il tuo CHMOD sia 777 –

+0

lo faccia nel php.ini – Gunnit

+0

No, vai semplicemente ai tuoi file con FTP e fai clic con il tasto destro e imposta i fileright al massimo (che è 777) –

risposta

32

Prova questa (metterlo prima di caricare un file)

//checking if file exsists 
if(file_exists("documenti/$fileName")) unlink("documenti/$fileName"); 

//Place it into your "uploads" folder mow using the move_uploaded_file() function 
move_uploaded_file($fileTmpLoc, "documenti/$fileName"); 
+0

sry ma la dose non funziona, e non so come impostare il permesso 777 – Gunnit

0

Hai provato a verificare se il file esiste ed eliminarlo se lo fa prima di spostare il file temporaneo nel percorso di archiviazione permanente?

+2

Questo deve essere un commento, non una domanda. – Bojangles

+0

no iam un auto pensiero phob noob quindi non sono sicuro di come farlo, ma mi guarderò, grazie per il consiglio – Gunnit

+0

Questo non dovrebbe essere pubblicato come risposta. D'accordo con @Bojangles. –

1

Forse lo script non ha i diritti per sovrascrivere? Prova a cambiare la directory su 777 e prova di nuovo. Se funziona, allora, si può capire il valore corretto è necessario

+0

lo faccio nel file php.ini – Gunnit

2
if (file_exists("documenti/$fileName")) 
{ 
unlink("documenti/$fileName"); 

echo "<font face='Verdana' size='2' >Last Uploaded File has been removed from uploads folder<br>back to uploadform agian and upload your file<br>";// now your file which uploaded before was deleted from uploads folder you can open it and check if it removed or not , so no you should go back to uploadform again and import your file which will uploaded correctly 

echo "<font face='Verdana' size='2' ><BR><BR><BR><a href='upform.php'>Back to upform</a><BR>"; 

} 
Problemi correlati