This code does not print the temporary name of the uploaded image as well does not upload any picture in my Photo folder. but print image name. it work others computer correctly . where is the problem? please help me. i am using php Version 5.6.20
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$destination = "Photo/".$_FILES['image']['name'];
$filename = $_FILES['image']['tmp_name'];
echo $destination;
echo "<br/>";
echo $filename;
echo "<br/>";
if (file_exists($destination))
echo "Sorry, file already exists.";
else
move_uploaded_file($filename, $destination);
$name=$_FILES['image']['name'];
$tmp_name=$_FILES['image']['tmp_name'];
echo $tmp_name;
}
?>
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<td>Choose Image</td>
<td><input type="file" name="image"/> </td>
</tr>
<tr>
<td>Description:</td>
<td><textarea name="Description" rows="4" cols="40"/></textarea></td>
</tr>
<tr>
<td></td>
<td> <input type="submit" name="submit_image" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<?php
if(isset($_POST['submit_image']))
{
$destination = "Photo/".$_FILES['image']['name'];
$filename = $_FILES['image']['tmp_name'];
echo $destination;
echo "<br/>";
echo $filename;
echo "<br/>";
if (file_exists($destination))
echo "Sorry, file already exists.";
else
move_uploaded_file($filename, $destination);
$name=$_FILES['image']['name'];
$tmp_name=$_FILES['image']['tmp_name'];
echo $tmp_name;
}
?>
<form method="POST" enctype="multipart/form-data">
<table>
<tr>
<td>Choose Image</td>
<td><input type="file" name="image"/> </td>
</tr>
<tr>
<td>Description:</td>
<td><textarea name="Description" rows="4" cols="40"/></textarea></td>
</tr>
<tr>
<td></td>
<td> <input type="submit" name="submit_image" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
I tried this code it is working for me. please give read write permission of your photo folder
I think it will work for you.
your code is working fine , check with 'Photo/' folder permission and tmp folder path too. ex : D:\xampp\tmp\phpDCD9.tmp.
Your code is perfectly working on my machine. I think you might want to check the directory name Photo to which the uploaded files are been stored. check your destination folder name inn your coding and defined folder name where your project is present are same.
Kindly post your error or warning message so that it is easy to help.
i think this part write properly like this it will solve
if (file_exists($destination)){
echo "Sorry, file already exists.";
}else{
move_uploaded_file($filename, $destination);
$name=$_FILES['image']['name'];
$tmp_name=$_FILES['image']['tmp_name'];
echo $tmp_name;
}
Related
I have this program where a puts in his/her name, the image's name, and selects the image from a file. Everything works perfectly but the image. It doesn't upload, nor does it get moved to the right folder. Here is the code for the user uploading his/her image:
<!DOCTYPE html>
<html>
<head>
<title>Upload pic to our site</title>
</head>
<body>
<form name="form1" method="post" action="check_image.php"
enctype="multipart/form-data">
<table border="0" cellpadding="5">
<tr>
<td>Image Title or Caption<br>
<em>Example: You talkin' to me?</em></td>
<td><input type="text" name="image_caption"
id="item_caption" size="55" maxlength="255"></td>
</tr>
<tr>
<td>Your Username</td>
<td><input type="text" name="image_username"
id="image_username" size="15" max="255"></td>
</tr>
<tr>
<td>Upload Image:</td>
<td><input type="file" name="image_filename"
id="image_filename"></td>
</tr>
</table>
<br>
<em>Acceptable image formats include: GIF, JPG, JPEG, PNG.</em>
<p align="center"><input type="submit" name="Submit"
value="Submit">
<input type="submit" name="Submit2" value="Clear Form">
</p>
</form>
</body>
</html>
Here is the code to show the image:
<?php
//connect to database
$link = mysqli_connect("localhost", "root", "", "moviesite");
if (!$link) {
"Connection lost: " . mysqli_connect_error();
}
//make variables available
$image_caption = $_POST['image_caption'];
$image_username = $_POST['image_username'];
$image_tempname = $_FILES['image_filename']['name'];
$today = date("Y-m-d");
//upload image and check for image type
$ImageDir = "/Applications/XAMPP/xamppfiles/htdocs/chapter7/images";
$ImageName = $ImageDir . $image_tempname;
if (move_uploaded_file($_FILES['image_filename']['tmp_name'],
$ImageName)) {
//get info about the image being uploaded
list($width, $height, $type, $attr) = getimagesize($ImageName);
switch ($type) {
case '1':
$ext = ".gif";
break;
case '2':
$ext = ".jpg";
break;
case '3':
$ext = ".png";
break;
default:
echo "Sorry, but the file you uploaded was not a GIF, JPG,
or PNG.";
echo "Please hit your browser's 'back' button and try
again.";
break;
}
//insert info into images
$insert = "INSERT INTO image
(`image_caption`, `image_username`, `image_date`)
VALUES
('$image_caption', '$image_username', '$today')";
$insertresults = mysqli_query($link, $insert);
$lastpicid = mysqli_insert_id($link);
$newfilename = $ImageDir . $lastpicid . $ext;
rename($ImageName, $newfilename);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Here is your pic</title>
</head>
<body>
<h1>So how does it feel to be famous?</h1><br><br>
<p>Here is your picture you just uploaded to our servers:</p>
<img src="<?php echo $ImageDir . $lastpicid . $ext; ?>" align="left">
<strong><?php echo $image_username; ?></strong>
This image is a <?php echo $ext; ?>image.<br>
It is <?php echo $width; ?> pixels wide
and <?php echo $height; ?> pixels high.<br>
It was uploaded <?php echo $today; ?>
</body>
</html>
The image is being saved into the "chapter7" folder. I want it to get saved in the "images" folder. The image is chosen from the "image" folder. Here is how the page looks like:
Image not showing on php page
If anyone can find a solution, that would help me alot! Thanks!
In HTML img tag the src attribute value can be only files available through web browser.
In your code you set $ImageDir = /Applications/XAMPP/xamppfiles/htdocs/chapter7/images so browser can't access directory and its files from this address because /Application/XAMPP... is not accessible by browser.
If you want to display this image you need to echo this path in src attribute of img tag:
echo "chapter7/images/" . $lastpicid . $ext;
and also change
$ImageDir to $ImageDir = "/Applications/XAMPP/xamppfiles/htdocs/chapter7/images/";
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 have a HTML form which is taking two entries and an image. I'm trying to upload the image to a "target" folder and then put a hyperlink to the image in my SQL database for retrieval in future, whilst also adding my other 2 entries form the form. I've followed a number of tutorials and I still can't seem to get this to work. At the moment the mySQL is working for the date and entry. Nothing is being entered into the table for picture, nor is the picture being uploaded to the target.
HTML code
<table border="0" cellpadding="3" cellspacing="1">
<form enctype="multipart/form-data" action="mob_create.php" method="post">
<tr>
<td width="150">Date:</td></tr><tr>
<td><input type="date" name="inputDate" value="" /> </td>
</tr>
<tr>
<td>Entry:</td></tr><tr>
<td width="300"><input size="34" type="text" name="inputEntry" value="" /></td>
</tr>
<tr>
<td width="150">Add an image (Optional):</td></tr><tr>
<td><input type="file" name="inputPic" accept="image/*" /></td></tr>
<tr>
<td><input type="submit" name="submit" /></td>
</tr>
</form>
</table>
PHP code
session_start();
include 'includes/Connect.php';
$target = "http://www.aam.prettypottery.ie/upload/";
$target = $target . basename( $_FILES['inputPic']['name']);
$date = $_POST['inputDate'];
$entry = $_POST['inputEntry'];
$picture = ($_FILES['inputPic']['name']);
$username=$_SESSION['myusername'];
if(!$_POST['submit']) {
echo "Please complete all entries of the form";
} else {
mysql_query("INSERT INTO `$username` (`Date`,`Entry`,`Picture`) VALUES('$date','$entry','$picture')") or die(mysql_error());
if(move_uploaded_file($_FILES['inputPic']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>
The path for the target needed to be local.
Also ensure that the folder in the target is writeable.
session_start();
include 'includes/Connect.php';
$target = "upload/";
$target = $target . basename( $_FILES['inputPic']['name']);
$date = $_POST['inputDate'];
$entry = $_POST['inputEntry'];
$picture = ($_FILES['inputPic']['name']);
$username=$_SESSION['myusername'];
if(!$_POST['submit']) {
echo "Please complete all entries of the form";
} else {
mysql_query("INSERT INTO `$username` (`Date`,`Entry`,`Picture`) VALUES('$date','$entry','$picture')") or die(mysql_error());
if(move_uploaded_file($_FILES['inputPic']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>
Why can't I do a proper strpos function on an uploaded file (I first upload the file, and then do a $site = stream_get_line($f, 4096, "\n")) and it works completely fine when read the same thing into $site from a manually created file on the server.
I have no idea what's causing it... it seems to read the line well in both cases, but strpos just doesn't work the same when it's uploaded via user versus when I create it manually.
Here's the code:
<?php
if(!$_FILES){
?>
<head>
<title>Bulk Index Checker</title>
</head>
<center>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br/>
<input type="submit" value="Check 'em!" />
</form>
</center>
<?php
}
else{
echo("<center> <h1>Checking... please wait.</h1> <br><table border=1> <tr><td>Site</td><td>Indexed</td></tr>");
$target_path = "checkupload/";
$target_path = $target_path . basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
$f = fopen($target_path, "r") or exit("Unable to open file!");
while (!feof($f))
{
$site = stream_get_line($f, 4096, "\n");
$url = 'http://www.google.com/search?q=info:' . $site;
//sleep(3);
$contents = file_get_contents($url);
if (strpos($contents,'<h3 class="r"><a href="/url?q='.$site)!=FALSE) {
echo("<tr bgcolor=\"Silver\"><td>".$site."</td><td>YES</td></tr>");
echo("<br>");
}
else{
echo("<tr><td>".$site."</td><td>NO</td></tr>");
}
}
?>
</table>
<br/>
<form>
</center>
<?php
}
?>
Edit: Here's another way I tried (using textarea) - same thing basically - it ONLY works properly when you input only 1 URL. As soon as you input another URL below, it shows a FALSE for the first one when it's really TRUE.
WHY IS THIS HAPPENING?
<?php
if(!$_POST){
?>
<head>
<title>Bulk Index Checker</title>
</head>
<center>
<h1>Bulk Index Checker v1.0</h1>
<form method="post" enctype="multipart/form-data">
<textarea id="list" name="list" rows="10" cols="50"></textarea>
<br/>
<input type="submit" value="Check 'em!" />
</form>
</center>
<?php
}
else{
echo("<center> <h1>Checking... please wait.</h1> <br><table border=1> <tr><td>Site</td><td>Indexed</td></tr>");
$lines = explode("\n", $_POST['list']);
foreach($lines as $site) {
echo($site);
$url = 'http://www.google.com/search?q=info:' . $site;
sleep(3);
$contents = file_get_contents($url);
if (strpos($contents,'<h3 class="r"><a href="/url?q='.$site)!=FALSE) {
echo("<tr bgcolor=\"Silver\"><td>".$site."</td><td>YES</td></tr>");
echo("<br>");
}
else{
echo("<tr><td>".$site."</td><td>NO</td></tr>");
}
}
?>
</table>
<br/>
</center>
<?php
}
?>
Try to use '!==' instead of '!='.
I don't have an option to test at this stage. See http://php.net/manual/en/function.strpos.php (example #2)
I am trying to Upload an image and store it in the database. I have mentioned the code, but it is not working for me. I'm not able to find the bug. Please see if anybody can find the bug.
The code is:
<?php
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
if($_REQUEST['submit'])
{
if(($_FILES['file']['type']=="image/jpeg" or $_FILES['file']['type']=="image/pjpeg" or $_FILES['file']['type']=="image/gif") and ($_FILES['file']['size']<300000))
{
if($_FILES['file']['error']>0)
{
$err=" ERROR :: ".$_FILES['file']['error'];
}
else
{
echo "Upload :".$_FILES['file']['name']."<br/>";
echo "Type: ".$_FILES['file']['type']."<br/>";
echo "Size: ".($_FILES['file']['size']/1024)."Kb<br/>";
echo "Stored in: ".$_FILES['file']['tmp_name']."<br/>";
move_uploaded_file($_FILES['file']['tmp_name'],"upload/".$_FILES['file']['name']);
echo $_FILES['file']['name'];
echo $_FILES['file']['tmp_name']."qwerty";
$n=3000;
$fo=fopen("upload/".$_FILES['file']['name'],"r");
$fr=fread($fo,$n);
mysql_query("update user set pic='$fr' where id='$id'");
}
}
else
{
$err=" ERROR :: Invalid Image";
}
mysql_query("update user set name='$name' where id='$id'");
mysql_query("update user set email='$email' where id='$id'");
}
$sql="select * from user where id='$id'";
$con=mysql_query($sql);
list($idR,$nameR,$emailR,$pwdR,$dateR,$picR)=mysql_fetch_array($con);
?>
<body>
<form action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<td colspan="2"><?php echo $err; ?></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" value="<?php echo $nameR ?>" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" value="<?php echo $emailR ?>" /></td>
</tr>
<tr>
<td><label for="file">Upload or Change Pic:</label></td>
<td><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="Save"/></td>
</tr>
</table>
</form>
</body>
My code is not even able to upload the file. When I tried to echo the following, I found them empty:
echo "Name:".$_FILES['file']['name']."<br/>";
echo "Type: ".$_FILES['file']['type']."<br/>";
echo "Size: ".($_FILES['file']['size']/1024)."Kb<br/>";
echo "Stored in: ".$_FILES['file']['tmp_name'];
All the above fields are empty.
I don't know how this work but it didn't worked. I guess the problem is somewhere in my HTML code because the $_FILES values are none/empty. It is not retrieving the value from the form tag please check if my answer is incorrect to solve the problem or correct to how to solved this hard problem from science or other subject in computers and it form to been error or successfully.
Try this!
<?php
if(isset($_POST['submit']) && $_FILES['file']['size'] > 0)
{
// Data
$file_name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
// Check
$type = array(
'image/jpeg',
'image/gif',
'image/png',
);
$size = 300000;
if(in_array($file_type, $type) AND $file_size > $size)
{
echo 'Ooops! File type must be JPG, GIF or PNG. Or file size too big';
exit();
}
// Is error?
if ($_FILES["file"]["error"] > 0)
{
echo 'Upload Error';
exit();
}
else
{
echo 'File Name : '.$file_name.'<br />';
echo 'File Size : '.($file_size / 1024).' Kb<br />';
echo 'File Type : '.$file_type;
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Save"/>
</form>
If $_POST and $_FILES are both empty then you've exceeded the maximum post size ini ('post_max_size')
There are several settings in php.ini, including the maximum for an individual file ('upload_max_filesize'), and the maximum for all uploads ('post_max_size'). Make sure those ini settings are set correctly
Be careful of syntax if you hve edited it, it's picky when handling M, K or G (not KB, MB or GB), and don't exceed the bit size of your server, i.e. don't go above 4billion bytes on a 32 bit server.
Other causes of $_FILES being empty, but not $_POST. You don't have enough free space to upload your files, or you don't have permissions ot write to the temp directory. Check /tmp has enough space, and that you can write to it as apache/php user.
It worked when I moved my code on an independent .php file. And when I needed to upload I gave the link to that page in which independent uploading code was written. I don't know the reason behind this but this is how I solved it.
hmm....
try to change this line
if($_REQUEST['submit'])
to be
if(isset($_REQUEST['submit']))
and i think, this code
if(($_FILES['file']['type']=="image/jpeg" or $_FILES['file']['type']=="image/pjpeg" or $_FILES['file']['type']=="image/gif") and ($_FILES['file']['size']<300000))
can be simplified into
if(ereg("jpeg|pjpeg|jpg|gif|png", $_FILES['file']['type']) AND $_FILES['file']['size']<300000)
Good luck...