Print images from a form using javascript or php - php

I have created a registration form which reads user data and send it to other php page which shows all the entered details. I was able to send its data to other php page using $_POST[] ,
but I am not able to send the images.
the below code is for the upload button which takes the image from user
<tr>
<td>Upload Image :</td>
<td><INPUT TYPE="file" NAME="image" required="required"></td>
</tr>
I have tried this using php but no luck
<? php
$img = $_POST['image'];
echo echo "<img src=$img>" ?>
So what could I do to send an image from one page to another using javascript or php?

The file selected by the user is sent over as data, so you have to pick up this data, save it somewhere, and then it is that "somewhere" that you would echo in an IMG tag.
W3Schools has an example of uploading and saving an image that you could adapt: http://www.w3schools.com/php/php_file_upload.asp

You have a problem in you uploading script.. To improve it:
HTML code:
<tr>
<td>Upload Image :</td>
<td>
<form action="" method="post" enctype="multipart/form-data" name="uploader" id="uploader">
<input type="file" name="image" required="required">
<input name="upl" type="submit" id="upl" value="Upload">
</form></td>
</tr>
PHP code:
if( $_POST['upl'] == 'Upload') {
$img = 'images/'.$_FILES['image']['name'];
if(#copy($_FILES['image']['tmp_name'],$img)) echo '<img src="'.$img.'">';
else echo 'ERROR!';
}

upload the file, move it from temp to a folder, display it from that folder
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Image:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
and
<? php
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "<img src= 'upload/" . $_FILES["file"]["name"] . "'>";
?>
done

Related

Appending Data to Uploaded File Using PHP

I'm making a PHP upload form, but need to add some additional data to each image based on what the user types in. For example, they choose the file to upload and type in the name, height, width, and price of that file, then hit submit. That information needs to be stored with the photo or appended to the metadata.
Here's what I have for submitFile.php:
<form enctype="multipart/form-data" action="Upload.php"
method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
Select a File: <input name="uploaded_file" type="file" /><br/>
<input type="submit" value="Upload" />
And here's what I have for Upload.php:
<?php
$uploaddir = "uploads/images";
$uploadfile = $uploaddir.basename($_FILES['uploaded_file']['name']);
echo "<pre style= font-size:20px>";
if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$uploadfile))
{
echo "<b>File has been successfully uploaded!</b>.\n";
}
else{
echo "<b>File upload failed!</b>.\n";
}
echo '<br/>Here is some more debugging info:'."<br/>";
echo "Name:".$_FILES["uploaded_file"]["name"]."<br/>";
echo "File Type:".$_FILES["uploaded_file"]["type"]."<br/>";
echo "File Size:".($_FILES["uploaded_file"]["size"]/1024)." Kb<br/>";
echo "Temp File:".$_FILES["uploaded_file"]["tmp_name"]."<br/>";
echo "</pre>";
?>
</form>
For JPEGs (and maybe TIFFs?), you should use something like this.
However, for PNGs, checkout this answer.

html form send data of upload file to php page, I want to handle it on php page

HTML code is below
Upload Your pic only in jpg (Less 1 MB)
<input type="file" title="Upload Your pic in jpg (Less 1 MB)" accept="image/jpeg" required="" class="select-style" tabindex="1" name="file[]" multiple />
PHP code is below:
echo $_POST[$_FILES['file[]']];
echo $_POST[$_FILES["file"]["size"]];
Your fomr must contain
enctype="multipart/form-data"
Like :-
<form name="upload" id="upload" method="post" action="" enctype="multipart/form-data">
You can use $_FILES['file']['size'][0]; to get size of uploaded file
Hope this will help you
Thanks
Ok..
here is my HTML Page page name stack.html
<form name="upload" id="upload" method="post" action="stack.php" enctype="multipart/form-data">
<input type="file" title="Upload Your pic in jpg (Less 1 MB)" accept="image/jpeg" required="" class="select-style" tabindex="1" name="file[]" multiple />
<input type="submit" value="submit">
</form>
and here my PHP page page name stack.php
<?php
if(!empty($_FILES)){
echo "<pre>";
print_r($_FILES);
echo "</pre>";
exit;
}
?>
See if it gives any luck. Try it in php file..
foreach($_FILES as $file){
print_r($file['size']);
print_r($file['name']);
//if you want total size of all images
echo array_sum($file['size']);
//if you want individual name
for($i=0;$i<count($file['name']);$i++){
echo $file['name'][$i];
echo $file['size'][$i];
}
}

upload multiple photos

I have a form which I am using to upload users photos but the problem is that I can upload 1 photos each time .like facebook I want my users to select multiple images in one shot. can anyone please guide. here is my present code.
<tr><td><input type="file" name="photos[]" /></td><td><input type="text" size="35" name="descriptions[]" /></td></tr>
and php is processing the uploaded images. can you please tell how should I do so that multiple images can be selected and uploaded in one shot
first thing is you need to make your form multipart
<form method="post" action="where_ever" enctype="multipart/form-data">
And if you use HTML5 the next part is to create a named array
<input type="file" accept='image/*' name="multiImageUpload[]" id="multiImageUpload" />
<input type="file" accept='image/*' name="multiImageUpload[]" id="multiImageUpload" />
<input type="file" accept='image/*' name="multiImageUpload[]" id="multiImageUpload" />
This will put all files into a $_POST array called multiImageUpload.
In order to allow a name to accept multiples in an array you need to use []
at the back of name, name[] or images[] or files[], also using the HTML5
property multiple multiple='' or multiple='multiple' will allow you
to select multiple files at once form a single input.
Here's some working sample code to play around with
The HTML
<form method="post" action="upload.php" enctype="multipart/form-data">
<input name='uploads[]' type="file" multiple=""/>
<input type="submit" value="Send">
</form>
The PHP
foreach ($_FILES['uploads']['name'] as $filename) {
echo '<li>' . $filename . '</li>';
}
// full contents of $_FILS
echo '<pre>';
var_export($_FILES);
echo '</pre>';

how to use target in onclick event

Hi iam new to php and html ,here i have one resolution,i just upload a picture in database as well as upload folder.and i put download button also.the download button is working to download as same window.but i have to show in new window.please see my code.
<input type="submit" onclick='this.form.action="download.php"'
name="agreement" id="butt_down"
value="<?php echo _l('Dowload'); ?>" class="submit_button">
download.php
foreach($_POST as $name=>$value)
{
echo karthik;
if($value=='Dowload')
{
if($name=='agreement'){
$filename=$_POST['filename1'];
}
if($name=='invoice'){
$filename=$_POST['filename2'];
}
}
}
$baseurl='uploads/'.$filename;
header("Location:$baseurl");
Wrap your input in a form. This way you can remove the javascript completely
<form action="download.php" method="post" target="_blank">
<input type="submit" name="agreement" id="butt_down" value="<?php echo _l('Dowload'); ?>" class="submit_button">
</form>

PHP APC Progress Bar

Post Updated: After commentors advice.
Index.php
<?php
$id = uniqid("");
?>
</head>
<body>
<form method="post" action="frame.php" target="upload_iframe" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $id; ?>"/>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
<iframe name="upload_iframe" style="width: 400px; height: 100px;">
</iframe>
frame.php
<?php
if(isset($_POST['progress_key'])) {
echo "hey1";
$status = apc_fetch('upload_'.$_POST['progress_key']);
echo $status['current']/$status['total']*100;
}
echo "hey2";
?>
Still doesnt work :(, I dont even get POST form data in frame. Where am i going so wrong?
Regards.
Whenever you use the APC file upload mechanism, you need to add an additional parameter to your form that identifies the file that's being uploaded, and is the key for your apc_fetch.
<?php $id = uniqid(time()); ?>
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="myUniProgressKey" value="<?php echo $id; ?>"/>
As the file is uploaded the value in the key upload . $id will contain the info you need to display the progress bar. Easiest way to get to is to ajax poll the server, using the apc_fetch call you have. This dictates that your upload page needs to not refresh the current page the user is on. I've used an iframe in the past that kicks off an interval to poll the server. Once the upload is complete, you're able to show a nice complete message in the same iframe.

Categories