Does anyone know of a good php or ajax multiple file upload script to upload to a web server?
The difference here is that nothing can be required on the client machine ie no flash!
I would like it to work just with the browser.
digitarald’s fancy upload
<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>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>
STEP2: Create file multiple_upload_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];
$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
//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);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][0]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][0]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][0]."<BR/>";
echo "<img src=\"$path1\" width=\"150\" height=\"150\">";
echo "<P>";
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][1]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][1]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][1]."<BR/>";
echo "<img src=\"$path2\" width=\"150\" height=\"150\">";
echo "<P>";
echo "File Name :".$HTTP_POST_FILES['ufile']['name'][2]."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size'][2]."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type'][2]."<BR/>";
echo "<img src=\"$path3\" width=\"150\" height=\"150\">";
///////////////////////////////////////////////////////
// Use this code to display the error or success.
$filesize1=$HTTP_POST_FILES['ufile']['size'][0];
$filesize2=$HTTP_POST_FILES['ufile']['size'][1];
$filesize3=$HTTP_POST_FILES['ufile']['size'][2];
if($filesize1 && $filesize2 && $filesize3 != 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($filesize3==0) {
echo "There're something error in your third file";
echo "<BR />";
}
?>
I found this code in http://www.phpeasystep.com/phptu/2.html
for more details check Tutorial
FancyUpload
SWFUpload
I'm a fan of plupload which support a wide variety of client technologies (html5, flash, silverlight, browserplus, gears) in addition to basic single file html4 upload, unlike SWFUpload which only does flash + html4 fallback.
It also integrates nicely with jQuery.
Related
i am uploading an image , image title is getting added to database but the file(image is not uploading/moving to the folder), i am getting 404 error for that image , i have set that folder permissions to 0777 and also max upload is 1024MB
$article_image = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
define ('SITE_ROOT', realpath(dirname('_FILE_')));
move_uploaded_file($image_tmp,SITE_ROOT.'/images/$article_image');
$add="insert into articles(article_title,article_date,article_author,category,article_image,article_keywords,article_content) values ('$article_title','$article_date','$article_author','$article_category','$article_image','$article_keywords','$article_content')" ;
if(mysqli_query($conn,$add)== 1 ){
echo "<script> alert('article added')</script>";
}
else{
echo "failed".mysqli_error($conn) ;
}
}
what mistake am i doing ?
EDIT here is my html code
<form method="post" action="addarticle.php" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="center"><h1> ADD ARTICLE</h1></td>
</tr>
<tr>
<td>Article Title</td>
<td><input type="text" name="title"></td>
<tr>
<td>Article Keyword</td>
<td><input type="text" name="keywords"></td>
<tr>
<td>Article Image</td>
<td><input type="file" name="image"></td>
</tr>
<td>Article Content</td>
<td><textarea name="content" cols="90" rows="30"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
Check your line with:
realpath(dirname('__FILE__'));
__FILE__
is a magic constant, and should not be wrapped in single or double quotes.
If you were to echo out the result of that function call you would probably see a different path than what you're expecting.
You're also trying to use string interpolation with single quotes around the variable instead of double:
SITE_ROOT.'/images/$article_image';
Should be:
SITE_ROOT."/images/$article_image";
Example:
if (!empty($_FILES['image'])) {
$tmp_file_to_upload = $_FILES['image'];
if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
$uploaded_name = $tmp_file_to_upload['name'];
$tmp_name = $tmp_file_to_upload['tmp_name'];
$destination = realpath(dirname(__FILE__))."images/$uploaded_name";
if (!move_uploaded_file($tmp_name, $destination)) {
die('Error uploading file.');
}
} else {
die('Error uploading file.');
}
}
Try this, Variable concatenation issue '/images/'.$article_image
move_uploaded_file($image_tmp,SITE_ROOT.'/images/'.$article_image);
instead of
move_uploaded_file($image_tmp,SITE_ROOT.'/images/$article_image');
I am trying to import csv file to database table using php. The problem is uploaded file is not recognized as csv file in the receiving page. Here is my code:
My form:
<form enctype='multipart/form-data' method='post' action="new_campaign.php" class="add_campaign_form">
<table class="add_campaign_table">
<tr>
<td><label>Campaign Name<label></td>
</tr>
<tr>
<td><input type="text" id="name" class="name" name="camp_name" value='' required/></td>
</tr>
<tr>
<td><label>Notes<label></td>
</tr>
<tr>
<td><textarea id="notes" name="camp_note" rows="4" cols="50" maxlength="250" placeholder="Campaign details" value='' required>
</textarea></td>
</tr>
<tr>
<td><label>Upload CSV File<label></td>
</tr>
<tr>
<td><input type="file" name="csv_file" id="csv" /></td>
</tr>
</table>
<input type="submit" class="submit" alt="Submit" width="120" height="30"/>
<br><br>
</form>
new_campaign.php
if(isset($_FILES) && $_FILES["file"]['error']==0){
if (($_FILES["file"]["type"] == "application/vnd.ms-excel")) {
if ($_FILES["file"]["error"] > 0) {
echo "error uploading the file";
}
else {
echo "hooray!";
}
}
else {
echo "this is not a csv file";
}
}
else{
echo "no files";
}
It keeps throwing me : "this is not a csv file"
I am getting the other field values in the receiving page. Any help?
Thanks Sean. Here is the working code:
if(isset($_FILES) && $_FILES["csv_file"]['error']==0){
//echo "file type: ".$_FILES["csv_file"]["type"];
if (($_FILES["csv_file"]["type"] == "text/csv")) {
if ($_FILES["text/csv"]["error"] > 0) {
echo "error uploading the file";
}
else {
echo "hooray!";
}
}
else {
echo "this is not a csv file";
}
}
else{
echo "no files";
}
Your input is name="csv_file"
<input type="file" name="csv_file" id="csv" />
So it should be
if(isset($_FILES) && $_FILES["csv_file"]['error']==0){
if (($_FILES["csv_file"]["type"] == "application/vnd.ms-excel")) {
if ($_FILES["csv_file"]["error"] > 0) {
...
not $_FILES["file"]['error']/$_FILES["file"]["type"]
I want to add the ID number of the row to the uploaded file file name.
e.g. if the file name is stack.pdf before uploading, after uploading it should change to stack-ID#.pdf.
This is the PHP Codes that is use to upload
$sp=mysqli_connect("localhost","root","","ara");
if($sp->connect_errno){
echo "Error <br/>".$sp->error;
}
$path="pdf/";
if(isset($_POST['upload']))
{
$path=$path.$_FILES['file_upload']['name'];
if(move_uploaded_file($_FILES['file_upload']['tmp_name'],$path))
{
echo " ".basename($_FILES['file_upload']['name'])." has been uploaded<br/>";
echo '<img src="gallery/'.$_FILES['file_upload']['name'].'" width="48" height="48"/>';
$img=$_FILES['file_upload']['name'];
$query="insert into library (path,CreatedTime) values('$img',now())";
if($sp->query($query)){
echo "<br/>Inserted to DB also";
}else{
echo "Error <br/>".$sp->error;
}
}
else
{
echo "There is an error,please retry or ckeck path";
}
}
And this is the form
<form action="accept-file.php" method="post" enctype="multipart/form-data">
<table width="384" border="1" align="center">
<tr>
<td width="108">Select File</td>
<td width="260"><label>
<input type="file" name="file_upload">
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="upload" value="Upload File">
</label></td>
<td> </td>
</tr>
</table>
</form>
I will really appreciate your help. Thanks.
Try this:
$uploadFileName = $_FILES['file_upload']['name'];
//get extention of upload file
$attachment_ext = explode('.', $uploadFileName);
$ext_pt = $attachment_ext[1];
//Give a new name for the file
$newName = '123'.$uploadFileName.".".$ext_pt;
$path = "YOURPATHHERE/";
$save_attchment = $path.$newName ; //setting the path
move_uploaded_file($_FILES['file_upload']['tmp_name'], $save_attchment);
Okay, this is what I got so far, I'm able to upload one image and display it in a div, but I just cant seem to figure out how to do the same for loading multiple images. As in upload multiple images and display all uploaded images on screen. Any help is appreciated, thanks in advance.
Also: I kinda think I have to set a variable for
$_FILES['image']['name'][0]
$_FILES['image']['name'][1]
etc
and do a forloop to print it out? Correct me if I am wrong?
<?php
// prevent timezone warnings
date_default_timezone_set('America/New_York');
// set the upload location
$UPLOADDIR = "tmp";
// if the form has been submitted then save and display the image(s)
if(isset($_POST['Submit'])){
// loop through the uploaded files
foreach ($_FILES as $key => $value){
$image_tmp = $value['tmp_name'];
$image = $value['name'];
$image_file = "{$UPLOADDIR}{$image}";
// move the file to the permanent location
if(move_uploaded_file($image_tmp,$image_file)){
echo <<<HEREDOC
<div style="float:left;margin-right:10px">
<img src="{$image_file}" alt="file not found" /></br>
</div>
HEREDOC;
}
else{
echo "<h1>image file upload failed, image too big after compression</h1>";
}
}
}
else{
?>
<form name='newad' method='post' enctype='multipart/form-data' action=''>
<table>
<tr>
<td><input type='file' name='image'></td>
</tr>
<tr>
<td><input name='Submit' type='submit' value='Upload image'></td>
</tr>
</table>
</form>
<?php
}
?>
I' am not sure which CMS/Framework this is but If you change this line
From
<input type='file' name='image'>
To
<input type='file' name='image[]' multiple>
Hope this helps you out.
How are multiple files, you must have two loops. Put a foreach inside a for.
I believe that is what you want. Make sure the tmp folder has write permission.
<?php
// prevent timezone warnings
date_default_timezone_set('America/New_York');
// set the upload location
$UPLOADDIR = "tmp";
// if the form has been submitted then save and display the image(s)
if(isset($_POST['Submit'])){
$num_files = count($_FILES['image']['tmp_name']);
for($x = 0; $x < $num_files; $x++){
$image = $_FILES['image']['name'][$x];
$image_file = $UPLOADDIR."/". $image;
if(!is_uploaded_file($_FILES['image']['tmp_name'][$x])){
$messages[] = '<h1>'.$image.' image file upload failed, image too big after compression</h1>."<br>"';
}
if (move_uploaded_file($_FILES["image"]["tmp_name"][$x],$image_file)){
echo '
<div style="float:left;margin-right:10px">
<img src="'.$image_file.'" alt="file" /></br>
</div>';
} else{
echo "<h1>image file upload failed, image too big after compression</h1>";
}
}
}else {
?>
<form name='newad' method='post' enctype='multipart/form-data' action=''>
<table>
<tr>
<td><input type='file' name='image[]'></td>
</tr>
<tr>
<td><input type='file' name='image[]'></td>
</tr>
<tr>
<td><input name='Submit' type='submit' value='Upload image'></td>
</tr>
</table>
</form>
<?php
}
?>
Below code is to update form details in database. It would update database only if image is changed/upload amother image. What should be done if we don't want to upload new image but need to update other form details? Thanks for solution, in advance!!!
<html>
<head>
<title>Update the Contact Record</title>
<link rel="stylesheet" type="text/css" href="cms_style.css">
<script>
window.onunload = function(){ window.opener.location.reload(); };
</script>
</head>
<body>
<h2 align="center">Update the Record</h2>
<?php
// error_reporting(~E_NOTICE);
//echo "Test 1 <br>";
$cid = $_GET['id'];
$uid = $_GET['uid'];
/* echo "<br> value of Con ID is : "; echo $cid;
echo "<br> value of UID is : "; echo $uid; */
if($uid==1) {
//echo "<br> Test 2 ";
updateRecord($cid);
} else if (isset($_GET['id']) ) {
//echo "<br>Test 3 ";
$ResumeID = $_GET['id'];
$sql="SELECT * from data WHERE ResumeID=$ResumeID";
$result = mysql_query($sql);
$Row=mysql_fetch_row($result);
?>
<form align="center" action="updateRecord.php?id=<? echo "$Row[0]"?>&uid=1" method="post" enctype="multipart/form-data">
<table align="center">
<input type="hidden" name="resumeid" value="<? echo "$Row[0]"?>">
<!-- <? echo "<tr> <td> Resume ID </td> <td>$Row[0]</td> </tr>" ?> -->
<div align="center">
<tr>
<td> Name of the Candidate</td>
<td><input type="text" name="NameoftheCandidate" size="25" value="<? echo "$Row[1]" ?>"></td>
</tr>
<tr>
<td>TelephoneNo</td>
<td><input type="text" name="TelephoneNo" size="25" value="<? echo "$Row[2]"?>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" size="25" value="<? echo "$Row[3]"?>"></td>
</tr>
<tr>
<td>WEYears</td>
<td><input type="text" name="WEYears" size="25" value="<? echo "$Row[4]"?>"></td>
</tr>
<tr>
<td>CurrentLocation</td>
<td><input type="text" name="CurrentLocation" size="25" value="<? echo "$Row[5]"?>"></td>
</tr>
<tr>
<td>PreferredLocation</td>
<td><input type="text" name="PreferredLocation" size="25" value="<? echo "$Row[6]"?>"></td>
</tr>
<tr>
<td>CurrentEmployer</td>
<td><input type="text" name="CurrentEmployer" size="25" value="<? echo "$Row[7]"?>"></td>
</tr>
<tr>
<td>CurrentDesignation</td>
<td><input type="text" name="CurrentDesignation" size="25" value="<? echo "$Row[8]"?>"></td>
</tr>
<tr>
<td>AnnualSalary</td>
<td><input type="text" name="AnnualSalary" size="25" value="<? echo "$Row[9]"?>"></td>
</tr>
<tr>
<td>UGCourse</td>
<td><input type="text" name="UGCourse" size="25" value="<? echo "$Row[10]"?>"></td>
</tr>
<tr>
<td> Image:
<? echo $Row[12]; ?> </td>
</tr>
<tr>
<? echo '<td><img src="http://localhost/cmsapp_latest/processimage.php?id=' . $Row[0] . '"></td>'; ?>
</tr>
<tr>
<td><input type="hidden" name="MAX_FILE_SIZE" value="10000000" />Change Image:</td>
<td><input name="userfile" type="file" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="submitvalue" value="UPDATE" ></td>
<td align="center"><input type="button" name="cancelvalue" value="CANCEL" onClick="self.close(); return false;"></td>
</tr>
</div>
</table>
</form>
<?php
} // end of else if
function updateRecord($cid) {
$msg = "Intial Value";
$maxsize = 10000000; //set to approx 10 MB
if($_FILES['userfile']['error']== UPLOAD_ERR_OK) {
echo "Print uplod error - ";
echo UPLOAD_ERR_OK;
//check whether file is uploaded with HTTP POST
if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
echo "tEST 02 - ";
//checks size of uploaded image on server side
if( $_FILES['userfile']['size'] < $maxsize) {
$finfo = finfo_open(FILEINFO_MIME);
if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {
echo "tEST 03 - ";
// prepare the image for insertion
$imgData =addslashes(file_get_contents($_FILES['userfile']['tmp_name']));
$sql= "UPDATE data SET NameoftheCandidate=\"$_POST[NameoftheCandidate]\", TelephoneNo='$_POST[TelephoneNo]', Email='$_POST[Email]', WEYears='$_POST[WEYears]',CurrentLocation='$_POST[CurrentLocation]', PreferredLocation='$_POST[PreferredLocation]', CurrentEmployer=\"$_POST[CurrentEmployer]\", CurrentDesignation='$_POST[CurrentDesignation]', AnnualSalary='$_POST[AnnualSalary]', UGCourse=\"$_POST[UGCourse]\", image=\"{$imgData}\", name=\"{$_FILES['userfile']['name']}\" WHERE ResumeID=$_GET[id]";
//echo $sql;
//$result = mysql_query($sql);
if(mysql_query($sql))
echo "Record updated";
else
echo "Record update failed";
}
else
$msg="<p>Uploaded file is not an image.</p>";
}
else {
// if the file is not less than the maximum allowed, print an error
$msg='<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is '.$maxsize.' bytes</div>
<div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].
' bytes</div><hr />';
}
}
else
$msg="File not uploaded successfully.";
}
else {
$msg= file_upload_error_message($_FILES['userfile']['error']);
}
return $msg;
} // end of update function
function file_upload_error_message($error_code) {
switch ($error_code) {
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk';
case UPLOAD_ERR_EXTENSION:
return 'File upload stopped by extension';
default:
return 'Unknown upload error';
}
}
?>
</body>
</html>
Hi I have done solution for this.Just add below code before
if($_FILES['userfile']['error']== UPLOAD_ERR_OK)
if($_FILES['userfile']['error']== UPLOAD_ERR_NO_FILE)
{
//echo UPLOAD_ERR_NO_FILE;
SQL update query
//echo $sql;
if(mysql_query($sql))
echo "Record updated";
else
echo "Record update failed";
}
Include below code in your php script
if ($_FILES["userfile"]["error"] == 0)
{
//update database code goes here
}
Create a hidden field for old image and populate with the value from database in your form.
$image=$_FILES["NEW-IMAGE-FIELD"]["name"];
if($image!="") {
SCRIPT TO UPLOAD NEW IMAGE
SCRIPT TO REMOVE OLD IMAGE
}
else
{
$image = $_REQUEST["HIDDEN-OLD-IMAGE-FIELD"];
}
$query = UPDATE DATABASE DETAIL WITH image='$image'";