Php Image Up load not working - php

I am trying to upload a image using PHP to a file, I am new to the langue so help is definitely appreciated. The image to be uploaded comes from a page with a from input, the code just doesn't seem to be executing! Thanks so much and here is my code (first html, second php):
I am trying to upload a image using PHP to a file, I am new to the langue so help is definitely appreciated. The image to be uploaded comes from a page with a from input, the code just doesn't seem to be executing! Thanks so much and here is my code (first html, second php):
<?php
include_once 'Includes/dbh.inc.php';
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $_SESSION['user']; ?></title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300|Tajawal|Quicksand|Raleway:100" rel="stylesheet">
<link rel="stylesheet" tyle="text/css" href="main.css">
</head>
<body>
<!-- START Header -->
<section class="headuser">
<div class="pagetitle">
C A T C H Y .
</div>
<div class="username">
<?php echo $_SESSION['user']; ?>
</div>
</section>
<!-- END Header -->
<!-- START Bio -->
<section class="Bio">
<div class="friends">
<br>freinds<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
<div class="editbio">
<?php echo $_SESSION['user']; ?>
<form action="Includes/upload.php" id="bioimage" method="POST" enctype="multipart/form-data">
<input type="file" name="profileup">
<button type="submit" id = "submit" name="upload">Use!</button>
</form>
<form action="Includes/Post.inc.php" id="bioform" method="POST">
<textarea name="Text1" id= "content" placeholder="Tell everyone who you are" cols="50" rows="10"></textarea>
<br>
<button type="submit" id="submit" name="submit">Post!</button>
</form>
</div>
</section>
<!-- END Bio -->
</body>
</html>
if(isset($_POST['submit'])) {
$file = $_FILES['profileup'];
$fileName = $_FILES['profileup']['name'];
$fileTmpName = $_FILES['profileup']['tmp_name'];
$fileSize = $_FILES['profileup']['size'];
$fileError = $_FILES['profileup']['error'];
$fileType = $_FILES['profileup']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed)){
if ($fileError === 0){
if ($fileSize < 1000000){
$filenamenew = uniqid('', true).".".$fileActualExt;
$filedest = 'profileimages/'.$filenamenew;
move_uploaded_file($fileTmpName, $filedest);
header("Location: ../UserProfile.php?suc");
}else{
echo "your file was to big";
}
}else{
echo "There was an error uploading your file";
}
}else{
echo "you cant upload files of this type";
}
}else{
header("Location: ../UserProfile.php?fail");
}
?>

I think you can use your button name "upload".
if(isset($_POST['submit']))
when you submit an image it must be working.
if(isset($_POST['upload']))

problem for not execution of php script in upload page is tthis line
if(isset($_POST['submit'])
because this check input submit is set in your html you have named submit button as upload
solution change name ofhttml buttton to submitt or change if condition to this
if(isset($_POST['upload']))

Related

Uploading photos in mysql and display using PHP

Hello guys, please help!
I'm having problems with my project, I followed these steps on how to upload(https://www.youtube.com/watch?v=Ipa9xAs_nTg) but something is wrong.
The error is Notice:
Undefined index: tmp_name in C:\xampp\htdocs\LDEVERACATERING\upload_process.php on line 16
Here is my upload.html
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.2.1.min"></script>
<script src="jquery-migrate-1.4.1.min"></script>
<title>
Image Upload
</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="content">
<form method="POST" action="upload_process.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000"/>
<div>
<input type="file" name="image"/>
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="Say something about this image..."></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image"/>
</div>
</form>
</div>
</body>
</html>
here is my upload_process.php
<?php
$msg = "";
//if upload button is pressed
if (isset($_POST['upload'])) {
//path to store the upload image
$target = "photos/".basename($_FILES['image']['name']);
//connect to database
$db = mysqli_connect("localhost", "root", "", "catering_info");
}
//get all the submitted date from the form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO photos_upload(image, text) VALUES('$image', '$text')";
mysqli_query($db, $sql); //stores the submitted date into the database table: images
//now let's move the upload image into the folder: photos
if (move_uploaded_file($_FILES['tmp_name']['name'], $target)) {
$msg = "Image uploaded successfully";
}
else
{
$msg = "There was a problem uploading image";
}
?>
PLEASE HELP ME, I REALLY NEED IT. Also I'm still new to this, I just started web prog last 3 weeks :( Thank you very much!
Please try this
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
Try this by replacing your code
move_uploaded_file($_FILES['tmp_name']['name'], $target)
With these
move_uploaded_file($_FILES['image']['tmp_name'], $target)

How to solve broken image displayed using php after upload to database

I try upload image to mysql database and display it along with the description of image using php. After i upload the image and display it , a broken image was displayed but the description of the image was displayed without any error. How can I solve this problem ? Appreciate your help
<?php
$msg = "";
//if upload button is pressed
if(isset($_POST['upload']))
{
// the path to store the uploaded image
$target = "images/".basename($_FILES['image']['name']);
// connect to database
$db = mysqli_connect("localhost","root","","product");
// Get all the submitted data from the form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO product_list (image, text) VALUES ('$image','$text')";
mysqli_query($db,$sql); // stores the submitted data into the database table : product_list
// move uploaded image to the folder : image
if (move_uploaded_file($_FILES['image']['tmp_name'],$target))
{
$msg = "Image and text uploaded successfully";
}else
{
$msg = "There was a problem uploading image";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload With Description</title>
<link rel="stylesheet" type="text/css" href="formstyle.css">
</head>
<body>
<div id="content">
<?php
$db = mysqli_connect("localhost","root","","product");
$sql = "SELECT * FROM product_list";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result))
{
echo "<div id='img_div'>";
echo "<img src='".$row['image']."'>";
echo "<p>".$row['text']."</p>";
echo "</div>";
}
?>
<form method="post" action="try.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="Details of product"></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image">
</div>
</form>
</div>
</body>
</html>
This is my result :
You are storing it in the DB without the images directory. You either need to store it with that, or always remember to call it that way in your image calls.
echo "<img src='images/".$row['image']."'>";
or make your the record you are writing the same as the filesystem location.
$image = 'images/' . $_FILES['image']['name'];
Note you are open to SQL injections and file inclusion injections with this code.
try this
<?php
$msg = "";
//if upload button is pressed
if(isset($_POST['upload']))
{
// the path to store the uploaded image
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . basename( $_FILES["image"]["name"]);
// connect to database
$db = mysqli_connect("localhost","root","","product");
// Get all the submitted data from the form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO product_list (image, text) VALUES ('$image','$text')";
mysqli_query($db,$sql); // stores the submitted data into the database table : product_list
//#move_uploaded_file($_FILES['image']['tmp_name'], $target_path)
// move uploaded image to the folder : image
if (move_uploaded_file($_FILES['image']['tmp_name'],$target_path))
{
$msg = "Image and text uploaded successfully";
}else
{
$msg = "There was a problem uploading image";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Image Upload With Description</title>
<link rel="stylesheet" type="text/css" href="formstyle.css">
</head>
<body>
<div id="content">
<?php
$db = mysqli_connect("localhost","root","","product");
$sql = "SELECT * FROM product_list";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($result))
{
echo "<div id='img_div'>";
echo "<img src='".$row['image']."'>";
echo "<p>".$row['text']."</p>";
echo "</div>";
}
?>
<form method="post" action="index.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000">
<div>
<input type="file" name="image">
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="Details of product"></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image">
</div>
</form>
</div>
</body>
</html>

Upload multiple files using PHP

I have the following code to upload files to a database in mysql:
Index.php
<?php
include_once 'dbconfig.php';
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Uploading With PHP and MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>File Uploading With PHP and MySql</label>
</div>
<div id="body">
<form action="upload.php" 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']))
{
?>
<label>File Uploaded Successfully... click here to view file.</label>
<?php
}
else if(isset($_GET['fail']))
{
?>
<label>Problem While File Uploading !</label>
<?php
}
else
{
?>
<?php
}
?>
Upload.php
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$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>
alert('successfully uploaded');
window.location.href='index.php?success';
</script>
<?php
}
else
{
?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php
}
}
?>
which works well, but only for individual file. I saw many tutorials, but I can not modify the code to allow upload multiple files at once. Any suggestions?
To allow multiple file uplodes you need to set attribute multiple="multiple" on the file input with a name file[]. Or you can use separate inputs with a name file[] as well. Than to store values in database make a
foreach($_FILES['filename']['name'] as $key=>$name){
// store each file and write data to DB for each file
//for example to get the name of file
$name = $_FILES['filename']['name'][$key];
}

Video file uploading

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.

Prevent form re-submit after refresh [duplicate]

This question already has answers here:
Does page reload ever cause post?
(3 answers)
Closed 9 years ago.
I've been reading some on other question regarding this that i should use the header( 'Locaction: xxx.php' ); but i can't figure out how to implement it to my code. I'm sorry for bad explaination on this. Any help or guiding i would be most greatful! This is the index.php below:
<body>
<div id="container">
<div id="upload">
<div id="logo"><img src="images/logo.png"></div>
<form enctype="multipart/form-data" method="post" action="uploader.php">
<p class="uploadtxt">Choose your file below:</p>
<input type="file" name="image" class="button" />
<input type="submit" value="Upload It!" class="button" />
</form>
</div>
<?php include 'footer.php'; ?>
</div>
</body>
</html>
And this is the uploader.php code below:
<?php
// Set local PHP vars from the POST vars sent from our form using the array
// of data that the $_FILES global variable contains for this uploaded file
$fileName = $_FILES["image"]["name"]; // The file name
$fileTmpLoc = $_FILES["image"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["image"]["type"]; // The type of file it is
$fileSize = $_FILES["image"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["image"]["error"]; // 0 for false... and 1 for true
$url = "http://localhost/";
// Specific Error Handling if you need to run error checking
if (!$fileTmpLoc) { // if file not chosen
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 10000000) { // if file is larger than we want to allow
echo "ERROR: Your file was larger than 10000000kB in file size.";
unlink($fileTmpLoc);
exit();
} else if (!preg_match("/.(gif|jpg|jpeg|png)$/i", $fileName) ) {
// This condition is only if you wish to allow uploading of specific file types
echo "ERROR: Your image was not .gif, .jpg, .jpeg or .png.";
unlink($fileTmpLoc);
exit();
}
//-- GENERATE A RANDOM NAME --//
$newfilename = rand(0, 999);
$newerfilename = $newfilename .'-'. $fileName;
//-- MAKE UPLOADS FOLDER IN YEAR AND MONTHLY --//
$path = "uploads/";
$year_folder = $path . date("Y");
$month_folder = $year_folder . '/' . date("m");
!file_exists($year_folder) && mkdir($year_folder , 0777);
!file_exists($month_folder) && mkdir($month_folder, 0777);
$path = $month_folder . '/';
move_uploaded_file($_FILES["image"]["tmp_name"], $path . $newerfilename);
?>
<html>
<head>
<title>Localhost - Upload Completed!</title>
<?php include_once 'header.php'; ?>
<body>
<div id="container">
<div id="upload">
<div id="logo"><img src="images/logo.png"></div>
<p class="filenametxt"><?php echo "The image is now uploaded!"; ?></p>
<p class="uploadtxt">Get the link below:</p>
<pre><?php echo $url . $path . $newerfilename; ?></pre>
</div>
<?php include 'footer.php'; ?>
</div>
</body>
</html>
Try this:
<input type="hidden" name="key" value="<?php echo (isset($_POST['key']) ? $_POST['key'] : rand(1,150)); ?>" />
<?php if (isset($_POST['key']) { $_SESSION['key'] = $_POST['key']); } ?>
And in your submission PHP:
<?php if (isset($_SESSION['key'])) { if ($_POST['key']==$_SESSION['key']){ echo "You may not resubmit a form!"; } } ?>

Categories