Upload multiple files using PHP - php

I have the following code to upload files to a database in mysql:
Index.php
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
<br /><br />
<?php
if(isset($_GET['success']))
{
?>
<label>File Uploaded Successfully... click here to view file.</label>
<?php
}
else if(isset($_GET['fail']))
{
?>
<label>Problem While File Uploading !</label>
<?php
}
else
{
?>
<?php
}
?>
Upload.php
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysql_query($sql);
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>
which works well, but only for individual file. I saw many tutorials, but I can not modify the code to allow upload multiple files at once. Any suggestions?

To allow multiple file uplodes you need to set attribute multiple="multiple" on the file input with a name file[]. Or you can use separate inputs with a name file[] as well. Than to store values in database make a
foreach($_FILES['filename']['name'] as $key=>$name){
// store each file and write data to DB for each file
//for example to get the name of file
$name = $_FILES['filename']['name'][$key];
}

Related

File Upload Issue Android WebView

How to upload an image to MySQL database from android WebView using PHP ? I have already implemented a file picker for choosing image from our device.
You can check this demo example
HTML
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="upload">upload</button>
</form>
</body>
</html>
PHP Code:
<?php
include_once 'database.php';
if(isset($_POST['upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="upload/";
/* new file size in KB */
$new_size = $file_size/1024;
/* new file size in KB */
/* make file name in lower case */
$new_file_name = strtolower($file);
/* make file name in lower case */
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO image(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysqli_query($conn,$sql);
echo "File sucessfully upload";
}
else
{
echo "Error.Please try again";
}
}
?>
Thanks

Php Image Up load not working

I am trying to upload a image using PHP to a file, I am new to the langue so help is definitely appreciated. The image to be uploaded comes from a page with a from input, the code just doesn't seem to be executing! Thanks so much and here is my code (first html, second php):
I am trying to upload a image using PHP to a file, I am new to the langue so help is definitely appreciated. The image to be uploaded comes from a page with a from input, the code just doesn't seem to be executing! Thanks so much and here is my code (first html, second php):
<?php
include_once 'Includes/dbh.inc.php';
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $_SESSION['user']; ?></title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300|Tajawal|Quicksand|Raleway:100" rel="stylesheet">
<link rel="stylesheet" tyle="text/css" href="main.css">
</head>
<body>
<!-- START Header -->
<section class="headuser">
<div class="pagetitle">
C A T C H Y .
</div>
<div class="username">
<?php echo $_SESSION['user']; ?>
</div>
</section>
<!-- END Header -->
<!-- START Bio -->
<section class="Bio">
<div class="friends">
<br>freinds<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
<div class="editbio">
<?php echo $_SESSION['user']; ?>
<form action="Includes/upload.php" id="bioimage" method="POST" enctype="multipart/form-data">
<input type="file" name="profileup">
<button type="submit" id = "submit" name="upload">Use!</button>
</form>
<form action="Includes/Post.inc.php" id="bioform" method="POST">
<textarea name="Text1" id= "content" placeholder="Tell everyone who you are" cols="50" rows="10"></textarea>
<br>
<button type="submit" id="submit" name="submit">Post!</button>
</form>
</div>
</section>
<!-- END Bio -->
</body>
</html>
if(isset($_POST['submit'])) {
$file = $_FILES['profileup'];
$fileName = $_FILES['profileup']['name'];
$fileTmpName = $_FILES['profileup']['tmp_name'];
$fileSize = $_FILES['profileup']['size'];
$fileError = $_FILES['profileup']['error'];
$fileType = $_FILES['profileup']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed)){
if ($fileError === 0){
if ($fileSize < 1000000){
$filenamenew = uniqid('', true).".".$fileActualExt;
$filedest = 'profileimages/'.$filenamenew;
move_uploaded_file($fileTmpName, $filedest);
header("Location: ../UserProfile.php?suc");
}else{
echo "your file was to big";
}
}else{
echo "There was an error uploading your file";
}
}else{
echo "you cant upload files of this type";
}
}else{
header("Location: ../UserProfile.php?fail");
}
?>
I think you can use your button name "upload".
if(isset($_POST['submit']))
when you submit an image it must be working.
if(isset($_POST['upload']))
problem for not execution of php script in upload page is tthis line
if(isset($_POST['submit'])
because this check input submit is set in your html you have named submit button as upload
solution change name ofhttml buttton to submitt or change if condition to this
if(isset($_POST['upload']))

upload file to mysql with PHP

I've really been stuggeling to get my head around the issue I have.
I've looked the whole internet but couldn't find any reason why I keep having this issue.
My file does get sent to the 'uploads' file on my server, but the details don't get sent to mysql db. What am I doing wrong?
Index.php
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="body">
<form action="upload.php" method="post" enctype="multipart/form-data">
<div class='container'>
<label for='voornaam' >Naam*: </label><br/>
<input type='text' name='voornaam' />
</div>
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
<br /><br />
</div>
</body>
</html>
upload.php
<?php
include 'dbconfig.php';
if($_FILES["file"]["error"]>0)
{
echo "FILE ERROR";
die();
}
$filename = "uploads/".$_FILES["file"]["name"];
// move file to a folder
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filename))
{
$sql="INSERT INTO tbl_uploads(name, file) VALUES('voornaam',$filename')";
mysql_query($sql);
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
?>
Firstly Drop using Mysql_* functions those are deprecated use
prepared statements instead
here's the solution
upload.php
<?php
include 'dbconfig.php';
if($_FILES["file"]["error"]>0)
{
echo "FILE ERROR";
die();
}
$filename = "uploads/".$_FILES["file"]["name"];
// move file to a folder
if(move_uploaded_file($_FILES["file"]["tmp_name"], $filename))
{
$sql="INSERT INTO tbl_uploads(name, file) VALUES('voornaam','$filename')";
mysqli_query($db,$sql);
?>
<script>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
?>
dbconfig.php
<?php
//header('Content-Type: text/html; charset=ISO-8859-1');
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'tbl_uploads');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
First of all, please read about PDO / MYSQLI.
About your problem, you have to remove '

Video file uploading

I'm new to php and I followed a tutorial that shows how to upload a video file.
At this moment it uses move_uploaded_file function but it doesn't work, the file is not shown in "videos" folder. Can somebody explain to me why the file isn't showing up?
<html>
<head>
<title>Video Upload System</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
include "connect.php";
?>
<div id='box'>
<form action="index.php" method="POST" enctype="multipart/form-data">
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['size'];
$random_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'MP4' && $type != 'flv'){
$message = "Video Foramt Not Supported!";
}else{
move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type);
$message = "Successfully Uploaded";
}
echo "$message <br/><br>";
}
?>
Select Video: <br/>
<input type='file' name='video' />
<br/><br/>
<input type='submit' value='Upload' />
</form>
</div>
<div id='box'>
<?php
?>
</div>
</body>
</html>
you can check if uploaded successfully..
if(move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type)) {
$message = "Successfully Uploaded";
}
I suspect the path you have provided is not valid, tough.
I think that you need to check the php.ini file to see the upload file (video) size limit and increase it, or just try to upload a small size file.

Outputting an image from an SQL BLOB

I have a php file which stores an image in a BLOB in a Database. The uploading part works fine. However it won't display the image and I don't know why. Help appreciated.
Two files:
index.php
<!DOCTYPE HTML>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<h1>Upload an Image</h1>
<form action="index.php" method="POST" enctype="multipart/form-data">
<label for="image">File:</label>
<input type="file" name="image">
<input type="submit" value="Upload">
</form>
<?php
//connect
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("up") or die(mysql_error());
//file stuff
$file= $_FILES['image']['tmp_name'];
if(!isset($file))
echo "Please select an image";
else {
$image=mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$imageName=mysql_real_escape_string($_FILES['image']['name']);
$imageSize=getimagesize($_FILES['image']['tmp_name']);
if(!$imageSize)
echo "Thats not an image";
else {
//upload
$query="INSERT INTO store VALUES('','$imageName','$image')";
$sendQuery=mysql_query($query);
if(!$sendQuery)
echo "This is embarressing. It didn't work";
else {
$lastid=mysql_insert_id();
echo "Image was uploaded. <br>Your image:";
echo "<img src=get.php?id=$lastid/>";
}
}
}
?>
</body>
</html>
and get.php:
<?php
//connect
mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("up") or die(mysql_error());
$id=mysql_real_escape_string($_REQUEST(['id']));
$imageQuery="SELECT * FROM store WHERE id=$id;";
$sendImageQuery=mysql_query($imageQuery);
$image=mysql_fetch_assoc($sendImageQuery);
$image=$image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
PHP uploads, as stored in the ['tmp_name'] are TEMPORARY files, which are automatically deleted by PHP when the script ends/exits, unless you've taken measures to move it elsewhere. You'd need to have something more like
if ($_FILES['image']['error'] != UPLOAD_ERR_OK) {
... handle upload ...
move_uploaded_file($_FILES['image']['tmp_name'], "/path/in/your/document/root/$id.jpg");
} else {
die("Upload failed with error code: " . $_FILES['image']['error']);
}
first. Note the addition of the error check - NEVER assume that an upload succeeded.

Categories