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 . '" />';
Related
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' />";
}
I'm building a website that has a form that allows users to upload files, but the form seems to be going to the wrong place. I told it to go to "upload.php", but instead it tries to go to "“upload.php", which I did not include in the form action. It doesn't look like I added any unicode characters in the script. Any reason why this might be happening?
Here's my form in index.html:
https://pastebin.com/7PETk5Sy
<!DOCTYPE html>
<html>
<head>
<title>Deeper</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="titlebar">
<img src="DeeperNetIcon.jpg"></img>
<h1>DeeperNet</h1>
Back to Deeper Homepage
<br/><br/>
<form action="view.php" method="post">Search for World by ID: <input type="text" name="world"> <input type="submit" value="Search"></form>
<br/>
</div>
<p color="white">Welcome to DeeperNet!</p>
<div id="upload">
<h1>Upload World to DeeperNet</h1>
<form action=“upload.php” method="post" enctype="multipart/form-data">
World File:
<br/><br/>
<input type="file" name="world">
<br/><br/>
World Name:
<br/>
<input type="text" name="name">
<br/>
Description:
<br/>
<input type="textbox" name="desc">
<br/><br/>
<input type="submit" value="Upload World">
</form>
<br/>
</div>
</body>
The index.html File has 2 Forms, the second form is the one I'm talking about.
Here's my upload.php Script:
https://pastebin.com/0FyuAX3Q
<?php
include("index.html");
if(!empty($_POST)) {
if(isset($_FILES["world"])) {
$world = $_FILES["world"];
$name = $world["name"];
$tmp_name = $world["tmp_name"];
$size = $world["size"];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
$allowed = "deep";
if ($ext == $allowed) {
if ($size <= 300000) {
$id = uniqid('', true);
$destination = 'worlds/' . $id . '.' . $ext;
if(move_uploaded_file($tmp_name, $destination)) {
echo '<br/>World: "' . $name . '" uploaded to DeeperNet!';
echo “<br/>World ID: ” . $id;
$info = fopen('worlds/' . $id . '.txt', 'w');
fwrite($info, $_POST['name'] . "\r\n");
fwrite($info, $_POST['desc']);
fclose($info);
}
}
}
MAMP says I'm using PHP 7.0.8 if you're wondering.
Replace your code with this as form accepts this double quotes (") not this one (“)
<form action="upload.php" method="post" enctype="multipart/form-data">
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>
I'm using a very simple php page to upload a file and display the image to same page; however, the image is not displaying. I checked whether the image was being uploaded and where, and it is being uploaded to the same directory as the php file.
<!doctype html>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>File Upload</h1>
<form method="post" action="upload.php" enctype="multipart/form-data">
Select File: <input type="file" name="filename" size="10" /><br/>
<input type="submit" value="upload" />
</form>
<?php
//checking if user uploaded image
if($_FILES) {
$name = $_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'], $name);
echo "Uploaded image $name <br/>";
echo "<img scr='$name' height='100px' width='100px'/>";
}
?>
</body>
</html>
echo "<img scr='$name' height='100px' width='100px'/>";
change to :
echo "<img src='{$name}' height='100px' width='100px'/>";
edit: Use curly brackets when including a variable in double quotes.
And please provide whole path of the image, like http://www.example.com/path/to/image/image.png
Please check your upload.php
Write code as shown below.
echo "<img src='" . $name . "' />";
when you write php variable in html ...you should concatenate that variable with .
I am having a small problem with file uploading.
So there is a button that you click and a window comes up( ajax powered), you select your file and press upload. The problem is as soon as the file gets uploaded to the browser the page reloads and closes the window. And there is no file :(. I have tested the codes without the window and they work greatly fine. What do you recommend?
here are my codes for upload
<html>
<body>
<h2 class="">Step 1: Add the video</h2>
<p class="note">Your text goes here</p>
<form method="post" enctype="multipart/form-data">
File Name:<input type="text" name="name"/>
<br/><br/>
Browse: <input type="file" name="file"/>
<br/><br/>
<input type="submit" id='button' Value="Upload the video file" name="submit"/>
</form>
<?php
if(isset($_POST["submit"]))
{
$filename=$_POST["name"];
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$size=$_FILES["file"]["size"];
$size=$size/1000000;
$size=round($size, 2);
$size.="MB";
$namewithext=$filename." (".$size.")".".".$ext;
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("../../../moreinfo/files/videos/" . $_FILES["file"]["name"]))
{
echo "<script> alert('".$_FILES["file"]["name"] . " already exists. "."');</script>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../../../moreinfo/files/videos/" . $namewithext);
echo "<script> alert('"."success"."');</script>";
}
}
}
else
{
}
?>
<?php
foreach(glob('../../../moreinfo/files/videos/*.*') as $filename){
$name = str_replace('../../../moreinfo/files/videos/', '', $filename);
$ext = pathinfo($name, PATHINFO_EXTENSION);
$notneeded=".".$ext;
$name = str_replace($notneeded, '', $name);
echo $name."</br>";
}
?>
</body>
</html>
Note that echo "<script> alert('"."success"."');</script>"; never gets executed because there is no time for that.
Thanks!
Replace
<form method="post" enctype="multipart/form-data">
with
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
if this is not issue then try
echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; exit;
and see if there is any error.