[英]php multiple file upload, file_exist
我是PHP编程的新手,我正在尝试制作多文件上传脚本,但是我不知道如何检查上传文件是否已存在! 我怎样才能做到这一点? 你能帮助我吗? 这是我的代码:
<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>'; } } } ?>
如果文件在目录中,则file_exists返回true。 因此,如果要检查file_exist的条件,则必须将move_uploaded_file保留在其中。
有关更多信息,请检查此文件是否存在-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>';
}
}
}
考虑到您给他们取了随机名称,这很难做到。 如果您使用的是数据库,请考虑保存文件md5并检查是否已拥有它。
否则,您可以使用md5作为文件名保存文件。
这是获取文件的md5的方法:
md5_file($tmp)
您可以尝试以下方法。 它使用一个简单的preg_match
函数调用来查看是否有一个名称相同的文件减去时间戳。
<?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 );
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.