upload image and showing the info - php

I was trying to upload a picture file and I wanted to display the information of that picture. But my code is not working, I'm not sure what's the problem, I hope someone could point out what am I doing wrong.
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="upload_image.php" action="post" enctype="multipart/form-data">
Select image: <input type="file" name="image">
<input type="submit" name="upload" value="Upload Now">
</form>
<?php
if(isset($_POST['upload'])){
echo $image_name = $_FILES['image']['name'];
echo $image_type = $_FILES['image']['type'];
echo $image_size = $_FILES['image']['size'];
echo $image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name == ""){
echo "<script>alert('Please select an image!')</script>";
exit();
}
}
?>
</body>
</html>
ps: I'm still new to php and file upload, so please go easy on me..Thank you in advance

The problem appears to be in the fact that you didn't specify any location to where to upload it, neither the file type. Here is the code with Extension verification, Size, and moving the file in a folder called images.
<?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'])));
$extensions = array("jpeg","jpg","png");
if(in_array($file_ext,$extensions )=== 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);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>

Related

rename file to current date before upload with php

i just moved from asp to php and i'm able to upload files to the server correclty. Now what i want to do is to rename the file before the upload is done and also echo the newly named file
<?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,"../complains_photos/".$file_name);
echo $file_type;
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
Just change the name that you are passing to the move_uploaded_file function. Something like this:
$destinationFileName = date('Ymd').'.'.$file_ext;
move_uploaded_file($file_tmp,"../complains_photos/".$destinationFileName);
Also change your echo to send $destinationFileName
Change move_uploaded_file(...); statement in the following way,
move_uploaded_file($file_tmp,"../complains_photos/".date("d-m-Y").".".$file_ext);
You can change the date format as per your preference.
Reference: http://php.net/manual/en/function.date.php

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 upload a quakeml file using PHP?

I am trying to upload a quakeml file type format. It needs to be stored in a directory via a form but it does not upload anything. I have already written this code below.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<body>
<?php
if(isset($_FILES['file'])){
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$expensions= array("quakeml");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a quakeml";
}
if($file_size > 4097152) {
$errors[]='File size must be excately 4 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</body>
</html>
Why is the above not uploading anything?

PHP file upload only retaining extension

For my website, I have a PHP image file upload system so that users can upload their own pictures. Those pictures are stored in /images/, and I have changed its file permission to 777 . When I test the upload file, it works but the file only keeps the .jpg extension and not the prefix:
<?php
$name= 'filename';
$newname= '/images/'. $name. '.jpg';
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){
rename($file_tmp, $newname);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>`
I haven't actually seen rename before, but just trying it, I'll admit its behavior doesn't seem to be quite what I expect. Try move_uploaded_file instead.
Most importantly, I notice you were checking that the only image formats you accepted were png and jpeg, yet you were renaming them all to jpg. Why? Not all image viewers will open a png with a jpg extension.
<?php
$name= 'filename';
$newname= '/images/'. $name;//. '.jpg'; // why wouldn't you preserve the correct extension? i.e. $file_ext
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){
// // // use move_uploaded_file() instead // // //
//rename($file_tmp, $newname);
move_uploaded_file($file_tmp, "$newname.$file_ext");
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>

Can I print the file name and path when using a PDF uploader?

I have wrote a simple PDF uploader in PHP. It works fine and the PDF uploads fine. However I would like to print the file name and the file path (where it is being saved). On the upload.php form. Is this possible?
upload.php
<form action="../pdf/upload_pdf2.php" method="POST" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="image"/>
<label><?php echo strlen($filename ) > 0 ? $filename : "" ?> will be saved to /public_html/dev/pdf</label>
<p> </p>
<input type="submit" class="upload-button"/>
</form>
upload_pdf2.php
<?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'])));
$extensions = array("jpeg","jpg","png","pdf");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excatly 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../pdf/".$file_name);
echo "File uploaded sucessfully";
}else{
print_r($errors);
}
}
?>

Categories