上传文件PHP代码时自动重命名同一文件名
multiple_upload.php 下面的代码是multiple_upload.php,它有两个上传按钮和一个提交按钮。 它的实际图片:上传文件PHP代码时自动重命名同一文件名,php,Php,multiple_upload.php 下面的代码是multiple_upload.php,它有两个上传按钮和一个提交按钮。 它的实际图片: <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form action="multiple_upload_ac.php" method="post" enctype="mu
<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="multiple_upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>multiple Files Upload </strong></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="ufile[]" type="file" id="ufile[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
多个文件上传
选择文件
选择文件
多个_上传_ac.php
此代码将处理您放在这行代码中的文件,它将保存在我的上载文件夹中
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
// Use this code to display the error or success.
$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
if($filesize1 || $filesize2!= 0)
{
echo "We have recieved your files";
}
else {
echo "ERROR.....";
}
// What files that have a problem? (if found)
if($filesize1==0) {
echo "There're something error in your first file";
echo "<BR />";
}
if($filesize2==0) {
echo "There're something error in your second file";
echo "<BR />";
}
?>
用于在复制文件之前检查文件是否已存在
如果存在,请将文件名更改为所需名称,我还建议您在循环中执行所有这些操作,不要重复自己的操作
例如:
if( file_exists($path1) ) {
// change another filename to overwrite $path1
}
很抱歉,我忘了检查文件是否存在,即使它有一个计数,因此您将有一些更复杂的工作要做,而不是一个简单的如果,下面是编辑的代码:
for( $i = 0; isset($HTTP_POST_FILES['ufile']['name'][$i]); $i++ ) {
/*
* do things before copy
*/
$path = 'upload/'.$HTTP_POST_FILES['ufile']['name'][$i];
$lastDot = strrpos($path, '.');
$base = substr($file, 0, $lastDot);
$ext = substr($file, $lastDot);
// this loop will keep doing until find a path that no file exists
for( $seq = 0; file_exists($path); $seq++ ) {
if( file_exists($path) ) {
$path = $base . '_'. $seq. $ext;
}
}
/*
* do copy and something else
*/
}
用于在复制文件之前检查文件是否已存在
如果存在,请将文件名更改为所需名称,我还建议您在循环中执行所有这些操作,不要重复自己的操作
例如:
if( file_exists($path1) ) {
// change another filename to overwrite $path1
}
很抱歉,我忘了检查文件是否存在,即使它有一个计数,因此您将有一些更复杂的工作要做,而不是一个简单的如果,下面是编辑的代码:
for( $i = 0; isset($HTTP_POST_FILES['ufile']['name'][$i]); $i++ ) {
/*
* do things before copy
*/
$path = 'upload/'.$HTTP_POST_FILES['ufile']['name'][$i];
$lastDot = strrpos($path, '.');
$base = substr($file, 0, $lastDot);
$ext = substr($file, $lastDot);
// this loop will keep doing until find a path that no file exists
for( $seq = 0; file_exists($path); $seq++ ) {
if( file_exists($path) ) {
$path = $base . '_'. $seq. $ext;
}
}
/*
* do copy and something else
*/
}
您可以使用在复制前进行检查,并查看文件信息,例如:
$file = $HTTP_POST_FILES['ufile']['tmp_name'][0];
$path_info = pathinfo($file);
$count = 0;
while(file_exists($file) {
$count ++;
$file = $path_info['dirname'] . $path_info['filename'] '_' . $count . '.' $path_info['extension'];
}
copy($file, $path) ;
您可以使用在复制前进行检查,并查看文件信息,例如:
$file = $HTTP_POST_FILES['ufile']['tmp_name'][0];
$path_info = pathinfo($file);
$count = 0;
while(file_exists($file) {
$count ++;
$file = $path_info['dirname'] . $path_info['filename'] '_' . $count . '.' $path_info['extension'];
}
copy($file, $path) ;
你想要什么确切的密码?只需在if表达式中执行一些字符串操作。您最好停止使用$HTTP_POST_文件,因为它已被弃用。显然,在复制文件后,您可以处理所有您想要的内容,就像我在代码do copy和其他一些
中所评论的那样,我不认为我应该为此回答一段代码,如果你有一个与之相关的数据库,你应该知道怎么做。你想要什么确切的代码?只需在if表达式中执行一些字符串操作。您最好停止使用$HTTP_POST_文件,因为它已被弃用。显然,在复制文件后,您可以处理所有您想要的内容,就像我在代码do copy和其他一些
中所评论的那样,我不认为我应该为此回答一段代码,如果你有一个与之相关的数据库,你应该知道怎么做。