I'm new to programming in PHP, and I'm trying to make a multiple file upload script, but I don't know how to check if the uploading files already exist! How can I do that? Can you help me? This is my code:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload">
</form>
foreach($_FILES['files']['name'] as $i => $name) {
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$explode = explode('.', $name);
$ext = end($explode);
$path = 'uploads/';
$path = $path . basename( $explode[0] . time() .'.'. $ext);
$errors = array();
if(empty($_FILES['files']['tmp_name'][$i])) {
$errors[] = 'Please choose at least 1 file to be uploaded.';
}
if(empty($errors)) {
if(!file_exists('uploads')) {
mkdir('uploads', 0777);
}
if(move_uploaded_file($tmp, $path)) {
echo '<p>The file <b>'.$name.'</b> successfully uploaded</p>';
}else {
echo 'Something went wrong while uploading the file <b>'.$name.'</b>';
}
}else {
foreach($errors as $error) {
echo '<p>'.$error.'<p>';
}
}
}
?>
</div>
file_exists return true if file is there in directory. So, you have to keep move_uploaded_file inside that if condition where file_exists you are checking.
For more info, check this File Exist - W3 Schools
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input type="submit" value="Upload">
</form>
foreach($_FILES['files']['name'] as $i => $name)
{
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$explode = explode('.', $name);
$ext = end($explode);
$path = 'uploads/';
$path = $path . basename( $explode[0] . time() .'.'. $ext);
$errors = array();
if(empty($_FILES['files']['tmp_name'][$i])) {
$errors[] = 'Please choose at least 1 file to be uploaded.';
}
if(empty($errors))
{
if(!file_exists($path))
{
if(move_uploaded_file($tmp, $path))
{
echo '<p>The file <b>'.$name.'</b> successfully uploaded</p>';
}
else
{
echo 'Something went wrong while uploading the file <b>'.$name.'</b>';
}
}
}
else
{
foreach($errors as $error)
{
echo '<p>'.$error.'<p>';
}
}
}
It's pretty hard to do it, considering that you're giving them random names. If you're using a database, considering saving the file md5 and check if you already have it.
Otherwise you could save the file with its md5 as the file name.
This is how you can get the file's md5:
md5_file($tmp)
You could try something along the lines of the following. It uses a simple preg_match
function call to see if there is a file that has the same name minus the timestamp.
<?php
$errors = array();
$path = 'uploads/';
/* Check if the target directory exists - no need to do it repeatedly */
if( !file_exists( $path ) ) mkdir( $path, 0777 );
/* file_exists results are cached */
clearstatcache();
foreach( $_FILES['files']['name'] as $i => $name ) {
if( !empty( $_FILES['files']['tmp_name'][$i] ) ) {
$name = $_FILES['files']['name'][$i];
$size = $_FILES['files']['size'][$i];
$type = $_FILES['files']['type'][$i];
$tmp = $_FILES['files']['tmp_name'][$i];
$ext=pathinfo( $name, PATHINFO_EXTENSION );
$basename=pathinfo( $name, PATHINFO_FILENAME );
$filepath = $path . $basename . time() . '.' . $ext;
$pttn='@'.$basename.'\d{10}@';
/* Does a file, with the name of the original, exist? */
if( preg_match( $pttn, implode( '', glob( $path .'*.*' ) ) ) ){
/* Files already exist */
} else {
$result=@move_uploaded_file( $tmp, $filepath );
echo $result ? '<p>The file <b>'.$name.'</b> successfully uploaded</p>' : 'Something went wrong while uploading the file <b>'.$name.'</b>';
}
} else {
$errors[] = 'Please choose at least 1 file to be uploaded.';
}
}
if( !empty( $errors ) ) echo implode( '<br />', $errors );
?>