I am new to php. I want to know how to change single image upload php to multi image upload.
Below is my code:-
<?php
require_once("header.php");
require_once("nav.php");
// Count total files
$countfiles = count($_FILES['files']['name']);
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frm_add_channel")) {
if(isset($_FILES['image']['tmp_name']))
{
$file = $_FILES['image']['tmp_name'];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES['image']['tmp_name'],"images/album" . $_FILES['image']['name']);
$albumid = $_POST['albumid'];
$name = str_replace("'","''",$name);
$place = $_POST['place'];
$place = str_replace("'","''",$place);
$owner = $_POST['owner'];
$type = $_POST['type'];
$path = $_FILES['image']['name'];
$sql = "INSERT INTO images (name,image,sys_album_id)
VALUES ('$path','$name','$albumid')";
$result = mysql_query($sql);
//$int_id = mysql_num_rows($result);
$int_id = mysql_num_rows();
echo '<script type="text/javascript">alert("Successfully Add New Channel!");location.href="gallery_upload.php";</script>';
}
}
?>
<br><br><br><br><br><br><br>
<?php echo $_GET['id'];?>
<form name="frm_add_channel" method="post" action='' enctype='multipart/form-data'>
<h4>Image (Less Than 2MB)</h4>
<img src="upload/qm.jpg" id="imgAvatar" alt="Course Image" />
<p><input type="file" name="image" id="image" onChange="showPreview(this)" accept="image/*" multiple /></p><br>
<input type="text" class="form" name="albumid" value"<?php echo $_GET['id'];?>" size="30" />
<p><input type="submit" value="Add Album" onclick="return checking()" /></p><br>
<input type="hidden" name="MM_insert" value="frm_add_channel">
</form>
try this below changed html & PHP file
$countfiles = count($_FILES['image']['name']);
for($i=0;$i<$countfiles;$i++)
{
$file = $_FILES['image']['tmp_name'][$i];
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'][$i]));
$image_name = addslashes($_FILES['image']['name'][$i]);
move_uploaded_file($_FILES['image']['tmp_name'][$i],"images/album" . $_FILES['image']['name'][$i]);
......
....
}
<form name="frm_add_channel" method="post" action='' enctype='multipart/form-data'>
<h4>Image (Less Than 2MB)</h4>
<img src="upload/qm.jpg" id="imgAvatar" alt="Course Image" />
<p><input type="file" name="image[]" id="image" onChange="showPreview(this)" multiple /></p><br>
<input type="text" class="form" name="albumid" value"<?php echo $_GET['id'];?>" size="30" />
<p><input type="submit" value="Add Album" onclick="return checking()" /></p><br>
<input type="hidden" name="MM_insert" value="frm_add_channel">
</form>
Related
I don't have an idea why it doesn't upload pictures(But the same peace of code does). Please help me.
HTML:
$max_filesize = 524288;
$upload_path = './img/';
$id=$_POST["id"];
for ($i=1; $i<=3; $i++)
{
(string)$inside="inside".$i;
echo $inside;
$filename = $_FILES[$inside]['name'];
echo $filename;
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
echo $ext;
if (isset($_POST[$inside])){
if(filesize($_FILES[$inside]['tmp_name']) > $max_filesize)
die('File is too big.');
if(!is_writable($upload_path))
die('Cannot access folder - 777.');
if(move_uploaded_file($_FILES[$inside]['tmp_name'],$upload_path . $id . "inside" . $i . $ext))
{
echo 'Uploaded successfuly ';
echo '<br><br>';
echo '<img src="' . $upload_path . $filename . '" width="300" >';
} else {
echo 'Something went wrong, try again.';
}
}
}
Before that:
<form method="post" enctype="multipart/form-data" action=""/>
Що в коробці(Фото): <p>
<input type="file" name="inside1"
accept=".jpg, .jpeg, .png"> <p>
<input type="file" name="inside2"
accept=".jpg, .jpeg, .png"> <p>
<input type="file" name="inside3"
accept=".jpg, .jpeg, .png"> <p>
</form>
It says 'Something went wrong, try again.'.
Strange form - <form></form>
<form action="<?php echo'admin/photo_upload.php'; ?>" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<p><input name="file_upload[]" type="file" id="file_upload[]" value=""></p>
<p><input name="file_upload[]" type="file" id="file_upload[]" value=""></p>
<p>Caption: <input type="text" name="caption" value="" /></p>
<input type="submit" name="submit" value="Upload" />
My problem is when the list input file is dynamic, so here is the code :
<?php
if(!empty($_FILES)){
$syarat = array('2','3','4','7');
$path = "./uploadtest/";
$jmlsyarat = count($syarat);
for($y = 0; $y<$jmlsyarat; $y++) {
foreach ($_FILES as $file){
$namafile = $syarat[$y].".jpg";
$tmpfile = $file['tmp_name'];
$kefile = $path.$namafile;
move_uploaded_file($tmpfile,$kefile);
}
}
}
?>
<form method="post" enctype="multipart/form-data" action="/uploadtest.php">
<input type="file" name="2" />
<input type="file" name="3" />
<input type="file" name="4" />
<input type="file" name="7" />
<input type="submit" />
</form>
Can it be done with dynamic handler that I create at above?
Thank you in advance
here is the work code (thx to #Federkun) :
<?php
if(!empty($_FILES)){
$path = "./uploadtest/";
foreach ($_FILES as $y => $file){
$namafile = $y.".jpg";
$tmpfile = $file['tmp_name'];
$kefile = $path.$namafile;
move_uploaded_file($tmpfile,$kefile);
}
}
?>
<form method="post" enctype="multipart/form-data" action="/uploadtest.php">
<input type="file" name="2" />
<input type="file" name="3" />
<input type="file" name="4" />
<input type="file" name="7" />
<input type="submit" />
</form>
That code work perfectly!
I'm programming a script that uploads mp3 to a folder in my server and adds the title, url and artist tot the 'audio' table.
but it dont work
PHP MySQL multiple mp3 upload in database
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
for($i=0;$i<count($_FILES["filUpload"]["name"]);$i++)
$target_dir = "/mounted-storage/home150/sub007/sc80538-VHHY/website.com/audio/files/";
$trackName = $_FILES['foto']['name'];
$titel = htmlspecialchars($_POST['titel']);
$artist = $_POST['artiest'];
{
if($_FILES["filUpload"]["name"][$i] != "")
{
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"][$i],"myfile/".$_FILES["filUpload"]["name"][$i]))
{
//*** Insert Record ***//
mysql_query("INSERT INTO `audio` (titel, url, categorie) values ('".$titel."', 'http://website.com/audio/files/".$trackName."','".$artist."')");
}
}
}
echo "Copy/Upload Complete<br>"; ?>
and the HTML form :
<form name="form1" method="post" action="upload.php" enctype="multipart/form-data">
Track1 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track2 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track3 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track4 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track5 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track6 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
<select name="artiest">
<?php
$query = mysql_query("SELECT * FROM artiesten ORDER BY naam ASC");
while ($array = mysql_fetch_assoc($query)){
echo "<option value=\"". $array['naam'] ."\">". $array['naam']. "</option>";
}
?>
</select>
<input name="btnSubmit" type="submit" value="Submit">
</form>
this is just a sample code you have to fit in your website and add mysql connection ware i leave comment or just include external file
php code
if (isset($_POST["upload"])){
$userdir = "mp3/songs/";
for($i=0; $i<count($_FILES['mp3']['name']); $i++) {
$target_file = $userdir . basename($_FILES["mp3"]["name"][$i]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (!file_exists($userdir)){
mkdir($userdir, 0777, true);
}
if (move_uploaded_file($_FILES["mp3"]["tmp_name"][$i], $target_file)) {
// your mysql connect code or you have to be included external file with $connect variable or just rename it
$song = $_FILES["mp3"]["name"][0];
$sql = sprintf(
"INSERT INTO music (mp3) VALUES ('%s')",
mysqli_escape_string($connect, $song)
);
$res = mysqli_query($connect,$sql);
echo "The file has been uploaded.\n";
} else {
echo "Sorry, there was an error uploading your file.\n";
}
}
}
html code :
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="mp3[]" multiple="true" >
<input type="submit" name="upload" value="upload">
</form>
I need to upload a file in a folder but the file never get in the folder. The name of the file is added correctly but no file in the folder. What is going wrong?
Php error: Undefined index: foto on line 12-16
$name= $_FILES["foto"]["name"];
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
$temp= $_FILES["foto"]["temp_name"];
$error= $_FILES["foto"]["error"];
if ($error > 0)
die("Error uploading file! code $error.");
else
{
if($type=="image/png" || $size > 2000000)//condition for the file
{
die("Format not allowed or file size too big!");
}
else
{
move_uploaded_file($temp,"assets/");
echo "Upload complete!";
}
}
HTML:
<form id="form" style="margin-left: 200px" action="addNewProduct.php" method="post"><br>
<br>
<br>
<div id="imageUpload">
<label for="foto">Foto</label>
<input type="file" name="foto" /><br>
</div>
<div id="infoForm">
<label for="productNaam">Productnaam</label>
<input type="text" name="productNaam"/><br>
<br>
<label for="beschrijving">Productbeschrijving</label>
<input type="text" name="productBeschrijving" /><br>
<br>
<label for="btw">BTW</label>
<input type="number" name="productBtw" /><br>
<br>
<label for="prijsinclbtw">PrijsInclBTW</label>
<input type="number" name="productPrijsInclBtw" /><br>
<br>
<br>
<br>
<input type="submit" name="submit" value="Add new Pen">
</div>
</form>
Your $_FILES["foto"]["temp_name"]; is incorrect, you should change it to $_FILES["foto"]["tmp_name"];
And your folder assets should exist in your root folder.
Your form should like this:
<form action="uploads.php" method="post" enctype="multipart/form-data">
<input type="file" name="foto">
<input type="submit" name="submit">
</form>
Please try this, hope this help you out : uploads.php
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
// $temp= $_FILES["foto"]["temp_name"];
$file_name = $_FILES["foto"]["name"];
$source = $_FILES['foto']['tmp_name'];
$dir = "./assets/";
$file = $dir . $file_name;
//$directory = "./assets/upload/$file_name";
if(!file_exists ($file ))
{
move_uploaded_file($source,$file );
exit();
}
else
{
echo "File exist";
}
When you upload files, form has to have enctype attribute.
<form enctype="multipart/form-data" id="form" style="margin-left: 200px" action="addNewProduct.php" method="post">
^^
I want to count set file upload. Here is my using code. Are there any better method to do this. Thanks.
<form action="index.php" method="post" enctype="multipart/form-data">
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<button name="submit" type="submit">Upload</button>
</form>
<?php
$img_error = '0';
$fill_img_count = '0';
if(isset($_POST['submit']))
{
$img_count = count($_FILES['new_image']);
echo "Total : ".$img_count."<br>";
for ($i=0 ; $i<=$img_count ; $i++)
{
if (isset($_FILES['new_image']) && !empty($_FILES['new_image']['name'][$i]))
{
$fill_img_count++;
}
}
echo "Set : ".$fill_img_count."<br>";
}
?>
$count_files = 0;
foreach ($_FILES['picture']['error'] as $item) {
if ($item != 4) {
$count_files++;
}
}
echo $count_files;
I'd recommend testing each ['error'] key against UPLOAD_ERR_OK.
You don't require to have name="new_image[]" as the name ... just new_image will suffice. If you post 1 or many, on the PHP side, you'll see $_FILES[]
Some useful links for you:
http://php.net/manual/en/reserved.variables.files.php
http://www.php.net/manual/en/features.file-upload.php
Some code:
if (empty($_FILES)) { echo "0 files uploaded"; }
else { echo count($_FILES) . " files uploaded"; }
Edit based on comment:
How to handle multiple file upload using PHP
From that post:
echo count($_FILES['file']['tmp_name']);
<?php
$count = 0;
foreach($_FILES['new_image']['error'] as $status){
if($status === UPLOAD_ERR_OK) {
$count++;
}
}
var_dump($count);
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<input name="new_image[]" type="file" />
<button name="submit" type="submit">Upload</button>
</form>