Hi every one i have make some php pages that is about web design presenter, and my problem is that, i receive image from user and save it into a folder and show all the saved images with href image link but , when i click on a image it will open only image in browser not in any page.
how to resolve this issue kindly help me.
my code is .
<html>
<head>
<link rel="stylesheet" type="css/text" href="style.css">
<title>Uploading Files via PHP</title>
</head>
<body>
<center><form action="file.php" method="post" enctype="multipart/form-data">
<table border="2">
<tr height="100"><td>Upload A file:</td>
<td><input type="file" name="file"></td></tr>
<tr height="100"><td>Click Upload File:</td><td><input type="submit" name="submit" value="Upload File"></td></tr>
</table>
</form></center>
<?php
if (isset($_POST['submit'])) {
$name=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$size=$_FILES['file']['size'];
$tmp=$_FILES['file']['tmp_name'];
if ($name==''){
echo "<script>alert('Please Select a file from computer!')</script>";
exit();
}
if (($type == "image/jpeg") || ($type == "image/gif") || ($type == "image/png")) {
if (file_exists("images/" . $_FILES["file"]["name"])) {
echo "This file $name is already exist!<br> please try another one..";
exit();
}
if ($size <= 50000){
move_uploaded_file($tmp,"images/$name");
echo "<center><font color=red>File was uploaded successfully!</font><br>Uploaded Image is here<br><img src='images/$name'> </center>";
}
else {
echo "Size of the image $size is larger than 50kb and it's not allowed... <br> image size must be less than 50kb..";
}
}
else {
echo "$type this is not a valid type of file<br>only upload Jpeg, PNG and Gif images";
}
}
?>
<div class="container">
<div class="row">
<?php
$files = glob("images/*.*");
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<div class="col-sm-4"><img src="'.$num.'" alt="random image" class="demo"/></div>';
}
?>
</div>
</div>
<script>
function fullwin(){
window.open("bigpage.php","bfs","fullscreen,scrollbars")
}
</script>
</body>
</html>
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 new to php and I followed a tutorial that shows how to upload a video file.
At this moment it uses move_uploaded_file function but it doesn't work, the file is not shown in "videos" folder. Can somebody explain to me why the file isn't showing up?
<html>
<head>
<title>Video Upload System</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
include "connect.php";
?>
<div id='box'>
<form action="index.php" method="POST" enctype="multipart/form-data">
<?php
if(isset($_FILES['video'])){
$name = $_FILES['video']['name'];
$type = explode('.', $name);
$type = end($type);
$size = $_FILES['video']['size'];
$random_name = rand();
$tmp = $_FILES['video']['tmp_name'];
if($type != 'mp4' && $type != 'MP4' && $type != 'flv'){
$message = "Video Foramt Not Supported!";
}else{
move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type);
$message = "Successfully Uploaded";
}
echo "$message <br/><br>";
}
?>
Select Video: <br/>
<input type='file' name='video' />
<br/><br/>
<input type='submit' value='Upload' />
</form>
</div>
<div id='box'>
<?php
?>
</div>
</body>
</html>
you can check if uploaded successfully..
if(move_uploaded_file($tmp, 'videos/'.$random_name.'.'.$type)) {
$message = "Successfully Uploaded";
}
I suspect the path you have provided is not valid, tough.
I think that you need to check the php.ini file to see the upload file (video) size limit and increase it, or just try to upload a small size file.
image uploading is successful in locally. But after hosting, it's not working.
$imgname=$_FILES["imgfile"]["name"];
if($imgname!=''){
if ((($_FILES["imgfile"]["type"] == "image/jpeg")
|| ($_FILES["imgfile"]["type"] == "image/jpg"))
&& ($_FILES["imgfile"]["size"] < 200000))
{
list($img)=explode("#",$mail); $img=$img.".jpg";
$extension=end(explode(".", $_FILES["imgfile"]["name"]));
move_uploaded_file($_FILES["imgfile"]["tmp_name"],"dpstaff/".$img);
} else { ?> <script type="text/javascript"> alert(" Type mismatch or Exceed Size "); window.location = "reg2.php"; </script> <?php }
} else { ?> <script type="text/javascript"> alert(" No Image Selected "); window.location = "reg2.php"; </script> <?php }
header("location:staf2.php");
}
else
{ ?>
<script type="text/javascript">
alert("Exist same value (mobile no.)");
</script>
<?php
}
and html code is :
<form method="post" action="staf1.php" enctype="multipart/form-data" >
<tr>
<td> Photo </td>
<td> <input type="file" name="imgfile" id="imgfile" <?php if(!(isset($_GET["edit"]))) echo "required"; ?> />
<mandatory>( < 200KB , jpg format) </mandatory></td>
</tr>
</form>
and there is one more problem, the message is not displaying.
How to display images right after they clicked the upload button?
Here is my HTML Code:
form.php
<html>
<body>
<form action="uploadFile.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
And here is my uploadFile.php code:(updated, but still not getting any result)
<?php
// prevent timezone warnings
date_default_timezone_set('Philippines/Manila');
// set the upload location
$UPLOADDIR = "images/";
// if the form has been submitted then save and display the image(s)
if(isset($_POST['Submit'])){
// loop through the uploaded files
foreach ($_FILES as $key => $value){
$image_tmp = $value['tmp_name'];
$image_type=$value['type'];
$image = $value['name'];
$image_file = "{$UPLOADDIR}{$image}";
//check if there's existing file name
if ($image != 0){
echo 'File Already Exists!';
}
else {
// move the file to the permanent location
if(move_uploaded_file($image_tmp,$image_file)){
// this is where the displaying part goes
echo <<<HEREDOC
<div style="float:left;margin-right:10px">
<img src="$editedShear" alt="This is your image" height=200 width=200/></br>
</div>
HEREDOC;
}
else{
echo "<h1>image file upload failed, image too big after compression</h1>";
}
}
}}
else{
?>
<?php
}
?>
To do that you need to upload the image using some kind of ajax upload. if you don't know what is that, please read here http://en.wikipedia.org/wiki/Ajax_%28programming%29. You can find some free solutions and examples online. Here I found a stack overflow question that might help you with links to such a solutions
Ajax file upload
Upload your image in a folder which contains your php/html file,
and in img tag write the img name:
<img src="<?php echo $img;?>" width="400" height="600"/>
/* i put image so that it has default
<img id="blah" src="images/male.png" alt="your image" style="width:250px;height:200px;" />
/*put accept and its format so that you can only choose the image only
<input type='file' name="image" onchange="readURL(this);" accept="image/gif,image/png,image/jpg,image/jpeg" />
/*changing the url of an image you can do it in jquery
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah')
.attr('src', e.target.result)
/*set the width and height of an image
.width(250)
.height(200);
};
reader.readAsDataURL(input.files[0]);
}
}
after that do your upload code
What is the easiest way to upload images and display the uploaded images on same page?
page "index.php"
<body>
<div id="body">
<form action="upload.php" name="img_compress" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
<br /><br />
<?php
if(isset($_GET['success']))
{
?><br><br><br>
<?php
$sql="SELECT * FROM tbl_uploads ORDER BY id DESC";
$result_set=mysql_query($sql);
while($row=mysql_fetch_array($result_set))
{
?>
<img src="uploads/<?php echo $row['file'] ?>" width="100%" height="100%">
<?php
}
?>
<?php
}
else if(isset($_GET['fail']))
{
?>
<label>Problem While File Uploading !</label>
<?php
}
else
{
?>
<label>Try to upload any files(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc...)</label>
<?php
}
?>
</div>
</body>
code for upload "upload.php"
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = $_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
$new_size = $file_size/1024;
$new_file_name = strtolower($file);
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
$sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
mysql_query($sql);
?>
<script>
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>
below one is fields for storing images
enter image description here
Add an iframe after your form:
<iframe name="resultFrame" width="500" height="300"></iframe>
And add the target="resultFrame" attribute in your form like this:
<html>
<body>
<form action="uploadFile.php" method="post"
enctype="multipart/form-data" target="resultFrame">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
<iframe name="resultFrame" width="500" height="300"></iframe>
</body>
</html>
iam new to php, i am using image upload script of php to update my logo, whenever user choose the image file the existing image file should be replaced by the new upoaded file and should be updated on the screen as well, for this iam using this script but its not doing anything, i mean its nor replacing the image neither putting the new image into the folder... :( please help me get out of this problem, i've been in this problem since months..
this is setup.php
<?php include("../includes/config.php"); ?>
<?php
if ($_SESSION["isadmin"])
{
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$result = mysql_query("SELECT * FROM setup WHERE (id=".$_SESSION["id"].")");
while($row = mysql_fetch_array($result))
{
$title = $row['title'];
$theme = $row['theme'];
}
mysql_close($con);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Admdin Home</title>
<link rel="StyleSheet" href="css/style.css" type="text/css" media="screen">
</head>
<body>
<?php include("includes/header.php"); ?>
<?php include("includes/nav.php"); ?>
<?php include("includes/aside.php"); ?>
<div id="maincontent">
<div id="breadcrumbs">
Home >
Setup >
Customization
</div>
<h2>Customize</h2>
<?php
if (isset($_GET["status"]))
{
if($_GET["status"]==1)
{
echo("<strong>Customization Done!</strong>");
}
if($_GET["status"]==2)
{
echo("<strong>Customization Error!!</strong>");
}
}
?>
<form method="post" action="setup-action.php" enctype="multipart/form-data" >
<label>Title Of Your Organization:</label> <input type="text" name="title" value="<? php echo $title; ?>" /> <br /> <br />
<label>Select Theme</label>
<select name="theme" value="<?php echo $theme; ?>">
<option value="Default">Default</option>
<option value="Dark">Dark</option>
<option value="White">White</option>
</select>
<br /> <br />
<label>Choose Your Logo Here</label><input type="file" name="file"/><br /> <br />
<input type="submit" name="Upload" value="Upload" />
</form>
</div>
</body>
<?php include("includes/footer.php"); ?>
</html>
<?php
}
else
{
header("Location: ".$fullpath."login/unauthorized.php");
}
?>
and this is setup-action.php
<?php include("../includes/config.php");?>
<?php
if(isset($_FILES["file"]))
{
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 1000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
if (file_exists("../graphics/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["name"],
"../graphics/" . $_FILES["file"]["name"]);
echo "Stored in: " . "../graphics/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
<?php
$title=$_POST["title"];
$theme=$_POST["theme"];
$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$result=mysql_query("SELECT * FROM setup WHERE id=".$_SESSION['id']);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0)
{
{
mysql_query("UPDATE setup SET title='".$title."' , theme='".$theme."'WHERE id=".$_SESSION['id']);
header("Location:setup.php?status=1");
}
}
else {
header("Location:setup.php?status=2");
}
mysql_close($con);
?>
See this URL
http://www.tizag.com/phpT/fileupload.php
Try this:-
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
uploder.php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
STEP1: Create file upload.php
############### Code
`<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>Single File Upload </strong></td>
</tr>
<tr>
<td>Select file
<input name="ufile" type="file" id="ufile" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
`
STEP2: Create file upload_ac.php
############### Code
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "upload/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";
//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
echo "<img src=\"$path\" width=\"150\" height=\"150\">";
}
else
{
echo "Error";
}
}
?>