Déplacer des fichiers uploader vers un dossier
sarrarsenal
-
Quo -
Quo -
Bonjour mes amis je voudrais savoir comment je pourrais faire pour déplacer les fichier uploader dans un répertoire que j appel upload qui est dans la racine du serveur localhost .en faite le serveur es localhost.voici le code que j ai ecrit mais je n arrive pas a placer les fichiers uploader au bon endroit
1) code pour le input
<form id="form5" name="form5" enctype="multipart/form-data" method="post" action="uploader.php">
<label>
<INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE
VALUE=1024>
<INPUT TYPE="FILE" NAME="userfile"><BR>
<td> <INPUT TYPE="SUBMIT"
value="Enregistrer le fichier"></td><br/>
</FORM>
</label>
</form>
2/code pour déplacer les fichiers au bon endroit
<?php
// Declare variables
// Get the basic file information
$userfile = $_FILES['userfile']['name'];
$file_size = $_FILES['userfile']['size'];
$file_temp = $_FILES['userfile']['tmp_name'];
$file_err = $_FILES['userfile']['error'];
$path = 'C:\wamp\www\mohasarrnewgref\upload';
// Create a new file name
// This is so if other files on the server have the same name, it will be renamed
$randomizer = rand(0000, 9999);
$file_name = $randomizer.$userfile;
// Get the file type
// Using $_FILES to get the file type is sometimes inaccurate, so we are going
// to get the extension ourselves from the name of the file
// This also eliminates having to worry about the MIME type
$file_type = $userfile;
$file_type_length = strlen($file_type) - 3;
$file_type = substr($file_type, $file_type_length);
if(!empty($userfile)) {
echo '<div style="font-weight: bold; padding: 6px;">File Uploaded Information</div>
<ul>
<li>Original File Name: ' .$userfile. '</li>
<li>New File Name: ' .$file_name. '</li>
<li>File Type: ' .$file_type.'</li>
<li>File Size: ' .$file_size. '</li>
<li>File Temporary Name: ' .$file_temp. '</li>
<li>Fille Error: ' . $file_err. '</li>
</ul>';
// limit the size of the file to 200KB
if($file_size > 25600) {
echo 'FILE SIZE TO LARGE<BR />';
exit();
}
// Set allowed file types
// Set case of all letters to lower case
$file_type = strtolower($file_type);
$files = array();
$files[] = 'jpeg';
$files[] = 'jpg';
$files[] = 'gif';
$files[] = 'png';
$files[] = 'txt';
$files[] = 'doc';
$files[] = 'xls';
// Search the array for the allowed file type
$key = array_search($file_type, $files);
if($key) {
echo '<b>Fichier Permis</b><br />';
} else {
echo '<b>Type De Fichier Incorrecte</b><br />';
exit();
}
// Check for errors and upload the file
$error_count = count($file_err);
if($error_count > 0) {
for($i = 0; $i <= $error_count; ++$i) {
echo $_FILES['userfile']['error'][$i];
}
} else {
if(move_uploaded_file($file_temp, 'C:\wamp\www\mohasarrnewgref\upload' .$file_name.'')) {
echo '<h3>Upload Successful!</h3>';
} else {
echo '<h3>Erreur De Chargement</h3>';
}
}
} else {
echo '<h3>No file has been selected.</h3>';
}
?>
1) code pour le input
<form id="form5" name="form5" enctype="multipart/form-data" method="post" action="uploader.php">
<label>
<INPUT TYPE=HIDDEN NAME=MAX_FILE_SIZE
VALUE=1024>
<INPUT TYPE="FILE" NAME="userfile"><BR>
<td> <INPUT TYPE="SUBMIT"
value="Enregistrer le fichier"></td><br/>
</FORM>
</label>
</form>
2/code pour déplacer les fichiers au bon endroit
<?php
// Declare variables
// Get the basic file information
$userfile = $_FILES['userfile']['name'];
$file_size = $_FILES['userfile']['size'];
$file_temp = $_FILES['userfile']['tmp_name'];
$file_err = $_FILES['userfile']['error'];
$path = 'C:\wamp\www\mohasarrnewgref\upload';
// Create a new file name
// This is so if other files on the server have the same name, it will be renamed
$randomizer = rand(0000, 9999);
$file_name = $randomizer.$userfile;
// Get the file type
// Using $_FILES to get the file type is sometimes inaccurate, so we are going
// to get the extension ourselves from the name of the file
// This also eliminates having to worry about the MIME type
$file_type = $userfile;
$file_type_length = strlen($file_type) - 3;
$file_type = substr($file_type, $file_type_length);
if(!empty($userfile)) {
echo '<div style="font-weight: bold; padding: 6px;">File Uploaded Information</div>
<ul>
<li>Original File Name: ' .$userfile. '</li>
<li>New File Name: ' .$file_name. '</li>
<li>File Type: ' .$file_type.'</li>
<li>File Size: ' .$file_size. '</li>
<li>File Temporary Name: ' .$file_temp. '</li>
<li>Fille Error: ' . $file_err. '</li>
</ul>';
// limit the size of the file to 200KB
if($file_size > 25600) {
echo 'FILE SIZE TO LARGE<BR />';
exit();
}
// Set allowed file types
// Set case of all letters to lower case
$file_type = strtolower($file_type);
$files = array();
$files[] = 'jpeg';
$files[] = 'jpg';
$files[] = 'gif';
$files[] = 'png';
$files[] = 'txt';
$files[] = 'doc';
$files[] = 'xls';
// Search the array for the allowed file type
$key = array_search($file_type, $files);
if($key) {
echo '<b>Fichier Permis</b><br />';
} else {
echo '<b>Type De Fichier Incorrecte</b><br />';
exit();
}
// Check for errors and upload the file
$error_count = count($file_err);
if($error_count > 0) {
for($i = 0; $i <= $error_count; ++$i) {
echo $_FILES['userfile']['error'][$i];
}
} else {
if(move_uploaded_file($file_temp, 'C:\wamp\www\mohasarrnewgref\upload' .$file_name.'')) {
echo '<h3>Upload Successful!</h3>';
} else {
echo '<h3>Erreur De Chargement</h3>';
}
}
} else {
echo '<h3>No file has been selected.</h3>';
}
?>
A voir également:
- Déplacer des fichiers uploader vers un dossier
- Déplacer colonne excel - Guide
- Dossier appdata - Guide
- Renommer des fichiers en masse - Guide
- Comment réduire la taille d'un fichier - Guide
- Comment ouvrir un fichier epub ? - Guide