upload.php的
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Choose a file: <input name="uploadedfile" type="file" /><br />
Choose a file1: <input name="uploadedfile1" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
表单upload.php
的图片uploader.php
mysql_select_db("test");
$target_path = "uploads/" . basename( $_FILES['uploadedfile']['name']);
$currentfile = $_FILES['uploadedfile']['name'];
$target_path1 = "upload1/" . basename( $_FILES['uploadedfile1']['name']);
$currentfile1 = basename( $_FILES['uploadedfile1']['name']);
$dbfiles = mysql_query("SELECT * FROM new WHERE amount='$currentfile' || amount='$currentfile1'");
if(mysql_num_rows($dbfiles) > 0 )
{
//what code should i place here??
}
else
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "file1: ".$_FILES['uploadedfile']['name']."<br>";
$file1 = basename( $_FILES['uploadedfile']['name']);
mysql_query("insert into new (amount) values('$file1')");
}
if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1))
{
echo "file2: ".basename( $_FILES['uploadedfile1']['name']);
$file2 = basename( $_FILES['uploadedfile1']['name']);
mysql_query("insert into new (amount) values('$file2')");
}
}
我应该在if if if(mysql_num_rows($dbfiles) > 0 ){ }
内放置什么代码,以便如果已经存在的文件的文件名已经存在,那么该文件应该自动更改其文件名,例如,存在abc.jpg ,然后有人上传一个新文件,但名称和类型与abc.jpg相同..然后新文件必须是,abc_1.jpg ...如果有人上传与abc.jpg相同,名称应该是abc_2.jpg ..
abc_1.jpg必须是保存到mysql的名称,而abc_1.jpg必须是保存到准备好的文件夹的文件.. thx
答案 0 :(得分:0)
试试这个
<?php
$target_path = "uploads/" . basename( $_FILES['uploadedfile']['name']);
$target_path1 = "uploads/" . basename( $_FILES['uploadedfile1']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1)) {
echo "The file ". basename( $_FILES['uploadedfile1']['name']).
" has been uploaded";
}
else{
echo "There was an error uploading the file1, please try again!";
}
?>
答案 1 :(得分:0)
用这样的东西测试......
<?php
$target_path = "uploads/" . basename( $_FILES['uploadedfile']['name']);
$target_path1 = "uploads/" . basename( $_FILES['uploadedfile1']['name']);
$file_saved = false;
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name'])." has been uploaded";
$file_saved = true;
}
if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1))
{
echo "The file ". basename( $_FILES['uploadedfile1']['name']).
" has been uploaded";
$file_saved = true;
}
if (!$file_saved)
{
echo "There was an error uploading the file, please try again!";
}
?>