Can't display image I uploaded in PHP - php

I'm trying to upload an image and display after uploading, the upload part works fine but image can't display.
Any answers?
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo <<<_END
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
_END;
if($_FILES){
$name = $_FILES['fupload']['name'];
move_uploaded_file($name = $_FILES['fupload']['tmp_name'], $name);
echo "<br><img src='$name'>";
}
?>
</body>
</html>
Browser:
Image can't display

nevermind, the problem is move_upload ($name=xxxx, $name), it means you assign to $name the tmp source !
here is a working code
<!DOCTYPE html>
<html>
<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
<?php if($_FILES)
{
$source=$_FILES['fupload']['tmp_name'];
$target1 = $_FILES['fupload']['name'];
move_uploaded_file($source,$target1);
?>
<br>
source=<?php echo htmlspecialchars($source);?>
<br>
target=<?php echo htmlspecialchars($target1);?>
<img src="<?php echo htmlspecialchars($target1);?>"
<?php
} // if $_FILES
?>
</body>
</html>

Ok, following comment, it seems $name point To à path not accessible for external user. Try a link like this $name="c:\path To your base path\www\et.png"
Edit: supposing you have a existing www folder , where you find your index.php. It may be called public.

Related

Trouble accessing HTML form data using PHP

I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>

PHP input file error, echo file name with Post method

I'm trying to take posted input file name, I'm not uploading anywhere.
I just need the name of posted filename so I'm trying this code;
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES['file'];
}
?>
If I change enctype="multipart/form-data" into form tag, it's ok, but I need this tag.
You still need the enctype attribute, as the files will not be available without it.
if (isset($_POST['submit'])) {
echo $_FILES['file']['name'];
}
use
echo $_FILES['file']['name'];
instead of
echo $_FILES['file'];
$_FILES['file'] contains array of properties of uploaded file. use print_r instead. It will work fine.
you can get file name like that
$name = $_FILES['file']['name'];
this code is working fine
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo "<pre>";
print_r($_FILES['file']) ;
}
?>

image inside database saved as BLOB name

I'm new to PHP. I've uploaded the image inside the database but the image is saved in the name BLOB. So it is difficult for me to retrieve the image back. Please help me out on how to save the image in its actual name.
Following is my code.
<?php
require_once 'config.php'
?>
<html>
<head>
<title> image</title>
<body>
<form action="hello.php" method="post" enctype="multipart/form-data">
<label> Profile Image </label><br><br>
<input type="file" name="image" id="image" required autocomplete="off"/>
<input type="submit" value="upload" name="upload" >
</form>
<?php
if(isset($_POST['upload']))
{
$filetmp=$_FILES["image"]["tmp_name"];
$filename=$_FILES["image"]["name"];
$filetype=$_FILES["image"]["type"];
$filepath="image/".$filename;
move_uploaded_file($filetmp,$filepath);
$sql="INSERT INTO image(img,path,type) VALUES('$filename','$filepath','$filetype')" ;
mysqli_query($db, $sql);
echo"image uploaded";
}
?>
</body>
</html>
This is the output:

Save image as session and display it

I'm trying to save a file/image as session and then display it..
here's the first page which contains the form:
<form method="post">
<input type="file" name="picture" value="upload" id="file" accept="image/*" onsubmit="return validateForm()">
</form>
Ok, so on then next I save the image as session and try to show it (which is unsuccessful)...
Code:
<?php
if (isset( $_POST['picture']))
$_SESSION['picture'] = $_POST['picture'];
echo "<img src=" $_SESSION['picture'] " border='0' /> "
?>
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['picture'] = $destination;
header('Location: display_picture.php');
display_picture.php
<?php
session_start();
?>
<div>
<img src="<?php echo $_SESSION['picture']; ?>" alt="picture"/>
</div>

Upload mp3 to Server PHP

I am trying to upload a small mp3 through a simple html form using PHP.
Here is my html:
<html>
<head></head>
<body>
<form action="upload_mp3.php" method="post" enctype="multipart/form-data">
<input type="file" name="mp3" />
<input type="submit"/>
</form>
</body>
</html>
Here is my php:
<?php
$mp3 = ($_FILES['mp3']['name']);
$target = "mp3/";
$target = $target . basename( $_FILES['mp3']['name']);
if(move_uploaded_file($_FILES['mp3']['tmp_name'], $target))
{ header("Status: 200"); }
else
{ echo "no";}
?>
Does anything look wrong with the code. Also could it be that my temp file isn't writable? If so can I get some instructions on how to make it writable.
Thanks!
Make sure your form tag has the enctype=multipart/form-data attribute set:
<form action="upload_mp3.php" method="POST" enctype="multipart/form-data">

Categories