2011-11-09 1475 views
6

Estoy haciendo una aplicación que permite a los usuarios cargar un archivo en un directorio a través de PHP.Archivo PHP cargar y sobrescribir el archivo con el mismo nombre

Tengo problemas porque la dosis no me permite sobrescribir los archivos con el mismo nombre. Por ejemplo, tengo un archivo llamado text.php y lo cargo, ahora cuando vuelvo y cambio el contenido del archivo text.php y lo cargo de nuevo en el servidor, todavía tengo la versión sin las ediciones. Sin embargo, si subo otro archivo, funciona. Así que simplemente no puedo sobrescribir los archivos.

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"; 

} 

¿Cómo puedo cambiar el código para que cuando subo un archivo con el mismo nombre que sobrescribe el archivo existente?

+0

¿Seguro es imposible encontrar sobrescrito, con el código de la norma el comportamiento en un servidor tipo LAMP es que el archivo se sobrescribirá. ASÍ QUE asegúrese de que su CHMOD es 777 –

+0

hago eso en php.ini – Gunnit

+0

No, solo vaya a sus archivos con FTP y haga clic derecho y configure los derechos de archivos al máximo (que es 777) –

Respuesta

32

probar este (la puso antes de cargar un archivo)

//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 pero la dosis no funciona, y no sé cómo configurar el permiso 777 – Gunnit

0

¿Ha intentado verificar si el archivo existe y eliminarlo si lo hace antes de mover el archivo temporal a la ubicación de almacenamiento permanente?

+2

Esto debe ser un comentario, no una respuesta. – Bojangles

+0

no iam un auto pensamiento php noob así que no estoy seguro de cómo hacer eso, pero iam va a investigar, gracias por el asesoramiento – Gunnit

+0

Esto no debe publicarse como una respuesta. De acuerdo con @Bojangles. –

1

¿Quizás la secuencia de comandos no tiene los derechos para sobrescribir? Intenta cambiar el directorio a 777 y prueba de nuevo. Si funciona entonces, se puede averiguar el valor correcto que necesita

+0

lo hago en el archivo 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>"; 

} 
Cuestiones relacionadas