Hi guys, I'm novice in php, so I really wish you guys would help me with my problem.
What I'm trying to do is upload some files (actually it's 3 files), rename it on the fly and then save some of it's informations to MySQL.
I know there's some questions has been asked in this forum about uploading file(s), and I've been trying to find the proper solution for my problem, but still couldn't find something that could help me nor I can understand how to use it. Just got me more errors and more confused, so I really hope you guys can help me.
Here's what I've done so far:
//The upload form
echo "
<form name='save01' enctype='multipart/form-data' method='post' action='?mode=save'>
<input name='files[]' type='file' id='files[]'><br>
<textarea name='infos[]' rows='3' id='titles[]'><br>
<textarea name='infos[]' rows='3' id='infos[]'><br><br>
<input name='files[]' type='file' id='files[]'><br>
<textarea name='infos[]' rows='3' id='titles[]'><br>
<textarea name='infos[]' rows='3' id='infos[]'><br><br>
<input name='files[]' type='file' id='files[]'><br>
<textarea name='infos[]' rows='3' id='titles[]'><br>
<textarea name='infos[]' rows='3' id='infos[]'><br><br>
<input type='submit' name='Submit' value='Upload Files!'
</form>
";
//The upload proccess
foreach ($_FILES["files"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$dsize = $_FILES["files"]["size"][$key];
$namA = $_FILES["fileku"]["name"][$key];
$namA_unik = uniqid(rand())."_".$namA; //i sepparate this so i can get the uniqe new name of the files
$folder = "../files/".$namA_unik;
move_uploaded_file($tmp_name, $folder);
}
}
With the script above i can manage to upload those files and now i'm trying to save some information of the uploaded files to my mysql table, so i change the like this:
foreach ($_FILES["files"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$dsize = $_FILES["files"]["size"][$key];
$namA = $_FILES["fileku"]["name"][$key];
$namA_unik = uniqid(rand())."_".$namA; //i sepparate this so i can get the uniqe new name of the files
$folder = "../files/".$namA_unik;
//changes start here
if (move_uploaded_file($tmp_name, $folder)) {
for ($count=0; $count<count($titles); $count++) { //to arraying titles
$titleA = $titles[$count];
for ($count=0; $count<count($infos); $count++) {
$infosA = $infos[$count];
$sql = "insert into files (file_name, file_size, file_title, file_info, ddate) VALUES ('$namA', '$dsize', '$titleA', '$infosA', now())";
$result=mysql_query($sql);
}
}
}
}
}
With this i can store the informations, but it inserted 3 times for each files what's wrong with my script? can u guys help me? I believe it was because I'm arraying ht 'titles' and 'infos' but I'm stuck with this, I can't fix it, can someone help me please...
Thanks alot in advance guys.