Upload Image and Display on Separate Page Using PHP - php

I'm stuck trying to get an uploaded image to display from an HTML form onto a PHP page.
All I want to do is have the image display on the second page.
Page One Code (HTML Form)
<form action="ShowReunionPhoto.php" method="POST" enctype="multipart/form-data">
Name: <input type="text" name="name" />
Description <input type="text" name="description" />
Upload Photo: <input type="file" name="reunionPhoto" />
<input type="submit" name="submit" value="Upload Photo" />
</form>
Page Two (PHP)
<?php
$Name = $_POST['name'];
$Description = $_POST['description'];
$Reunion = fopen("Reunion.txt", "ab"); {
fwrite($Reunion, $Name . "\n" . $Description . "\n");
fclose($Reunion);
}
$ReunionImage = $_FILES["file"]["reunionPhoto"];
echo '<img src="' . $ReunionImage . '">';
echo "<pre>\n";
echo readfile("Reunion.txt");
echo "</pre>\n";
?>

In order to make your image accessible, it needs to be copied to your project's directory.
Let's say you want to copy it to the images directory, relative to the script. Make sure your images directory exists.
$uploadedFile = $_FILES['reunionPhoto'];
$sourcePath = $uploadedFile['tmp_name'];
$destinationFileName = $uploadedFile['name'];
$destinationPath = __DIR__ . '/images/' . $destinationFileName;
move_uploaded_file($sourcePath, $destinationPath);
Then, in your <img> you can display that image.

you can even use the phpUpload library
$pUp = new phpUpload($_FILES['file']);
$pUp->maxSize('10');
$pUp->allowedType(["image/jpeg"]);
$pUp->newName("New_name");
if($pUp->run("test", true)){
echo "<img src='test/$pUp->output' />";
}

Related

Send file to server derictory over form with user's name

I have a form.html
<form action="user.php" method="post" enctype="multipart/form-data" >
<input type="text" size="40" name="UserCallFile" >
<input type="file" name="filename"><br>
<input type="submit" value="Send"><br>
</form>
And user.php
<?php
if(is_uploaded_file($_FILES["filename"]["tmp_name"]))
{
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_FILES["filename"]["name"]);
} else {
echo("Error");
}
?>
I need that user could download file to server which name of this file he written on text input. Ho to do that?
I tried this way
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_POST["fileName"] . "." . "png");
But of course here "png" have to be logic with any file extension.
I hope you are understand the idea of problem.
Simply try to play with this..
$image = $_FILES["filename"]["tmp_name"];
$dir = "gallery/"; //adjust this correctly, watch for slashes depending on your website config
$newname = $image['name'];
if(!#copy($image, $dir . $newname))
{
echo 'error';
}

How to Display image Using Session in PHP

i am able to display image on screen but i want to display the image using session.Please help me.
$_SESSION['user_name6'] = $_FILES["file"]["name"];
if(isset($_SESSION['user_name6']))
{
echo "<img src=<?php echo $_SESSION[user_name6]; ?> width=300 height=400
alt=Image path Invalid name=image />";
}
else
{
print "no pic here";
}
I do not know what you will actually do but this is the approach that best fits
when you put a file type in your form, you need to use the global variable Files
form.html
<form action="process.php" method="post" enctype="multipart/form-data">
<label for="picture">Picture:</label>
<input type="file" name="picture" id="picture"><br>
<input type="submit" name="submit" value="Upload">
</form>
process.php
<?php
session_start();
//make sure you have created the **upload** directory
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" . $_FILES["picture"]["name"];
move_uploaded_file($filename, $destination); //save uploaded picture in your directory
$_SESSION['user_name6'] = $destination;
header('Location: display_picture.php');
display_picture.php
<?php
session_start();
?>
<div>
<img src="<?php echo $_SESSION['user_name6']; ?>" alt="picture"/>
</div>

PHP Show image on the browser

test.php
<html>
<head></head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Attachment: <input type="file" name="file" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$_FILES['file']['name']);
echo ''.$name.'';
}
?>
From the above code, how do I show the images that I uploaded on the showfile.php?
You'd also need to pass the uploaded file path to showfile.php unless you have a pattern somehow. Say you decided to use $_GET, the link in your code would be
echo '<a href="showfile.php?file=uploads/' . $_FILES['file']['name']
. ' target="_blank">' . $name . '</a>';
In showfile.php you'd have something like this:
$file = $_GET['file'];
echo '<img src="uploads/' . $file . '" />';

File upload not posting data to iframe

I have a simple file upload form which posts to an iframe and the i retrieve the value from the iframe and redirect to another page.
The problem is the form isnt posting to the iframe (i dont think) so none of the php is being executed and null is being return to the javascript.
HTML Code
<form id="upload" name="upload" enctype="multipart/form-data" method="post" action="index.php" target="upload_target">
<input name="folder" type="hidden" value="<?php echo $folder ?>" />
<input name="filename" type="hidden" value="<?php echo $filename ?>" />
<input name="uploaded_file" type="file" size="5120" id="uploaded_file" accept="image/jpeg" />
<input id="sent" name="sent" type="submit" value="Upload" />
</form>
<iframe id="upload_target" name="upload_target" style="width:10px; height:10px; display:none"></iframe>
Javascript to get the value from iframe
$('form#upload').submit(function(){
$('#upload_wrapper').hide();
$('#loading').show();
// get the uploaded file name from the iframe
$('#upload_target').unbind().load( function()
{
var img = $('#upload_target').contents().find('#filename').html();
alert(img); //returning null
$('#loading').hide();
});
});
And the PHP at the top of the page to upload the file
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targetFolder = $_POST['folder'] . "/";
$filename2 = $_POST['filename'];
if (!is_dir($targetFolder))
{
mkdir($targetFolder, 0777);
}
$tempFile = $_FILES['uploaded_file']['tmp_name'];
$targetPath = dirname(__FILE__) . '/' . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['uploaded_file']['name'];
$uploadfile = $targetFolder. basename($filename2 .".jpg");
move_uploaded_file($tempFile,$uploadfile);
$fileName = $uploadfile;
echo "<div id='filename'>$fileName</div>";
}
This code has been working before so im unsure at why it has stopped now please help.

Multiple Upload using CTRL Key PHP

would you help for my code , i need to do the multiple upload but i cant so will you help me please. i need so bad.
here's my code
i made multiple upload the form but it is not working. the output is "error array"
//HTML
<html>
<head>
<form name="Image" enctype="multipart/form-data" action="upload.php" method="POST">
<h1><font face="tahoma"> UPLOAD FILES</h1>
<label for="file">Filename:</label>
<input type="file" name="Photo[]" accept="image/*" multiple="multiple"/><br/><br/>
<input type="hidden" id="pageName" name="pageName">
<script type="text/javascript">
//get page name from parent
var value = window.opener.pageName
document.getElementById("pageName").value = value;
</script>
<INPUT type="submit" class="button" name="Submit" value=" Upload ">
<INPUT type="reset" class="button" value="Cancel"><br/><br/>
</form>
</head>
</html>
//PHP this is were upload is do.
<?php
include('global.php');
?>
<?
$uploadDir = 'directory/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'][0];
$fileName1 = $_FILES['Photo']['name'][1];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName . $fileName1;
//upload error
if ($_FILES["Photo"]["error"] > 0)
{
echo "Error: " . $_FILES["Photo"]["error"] . "<br />";
}
//photo already exixts
else
//insert image into DB
{
move_uploaded_file($tmpName, $filePath);
$filePath = addslashes($filePath);
$filePath = stripslashes($filePath);
$filePath = mysql_real_escape_string($filePath);
$query = "INSERT INTO images (image , category ) VALUES ('$filePath', '$pageName')";
mysql_query($query) or die('Error, query failed');
echo" Upload Successful. <br/> <br/>";
echo "Stored in: " . "directory/" . $_FILES["Photo"]["name"];
?>
<br/><br/>
<img width="300" height="400" src="directory /<?=$_FILES["Photo"]["name"]?>"><br/>
<?
}
}
?>
Error: Array is telling you that the error being returned is actually an Array object, not a string. If you want to see the actual error message, you need to view the full contents of the array. Something like print_r($_FILES["Photo"]["error"]); or looping through the array like so
foreach($_FILES["Photo"]["error"] as $err) {
echo "error: " . $err . "<br>";
}
Or you can just print the first error returned just as you have returned the first file in your name array echo $_FILES["Photo"]["error"][0];

Categories