Shiyloh Fisher Shiyloh Fisher - 4 years ago 69
PHP Question

How to upload photo and display in folder and page using php?

I'm trying to make where you can upload a photo and show in folder. Then when you go to the page it will show you All photo's that were uploaded. I'm using PHP and HTML for this project.
Hopefully You understand my project by now.

This is my php code

<?php


if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}
else{
print_r($errors);
}
}
?>
<html>
<link rel="stylesheet" type="text/css" href="css/main1.css"/>
<body>
<?php include("includes/navigation.php"); ?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>

</body>
</html>


To show on page:

<?php
$dirname = "uploads/";
$images = glob($dirname."*.*");
?>

<?php
foreach($images as $image) {
echo '<img src="'.$image.'" /><br /><br />';
}
?>


Help will be appreciated. Thanks,

Answer Source
<?php
$dir = './';
$name = $_FILES['image']['name'];
$fullpath = $dir.$name;
$extension = pathinfo($name, PATHINFO_EXTENSION);
$tmpFilename = pathinfo($name, PATHINFO_FILENAME);
$i = 1;

while(file_exists($fullpath)){
    $_FILES['image']['name'] = $tmpFilename.'('.$i.')'.$extension;
    $fullpath = $dir.$_FILES['image']['name'];
    $i++;
 }
    move_uploaded_file($_FILES('image']['tmp_name'], $fullpath);
?>

In your form action attribute put:

upload_photo.php

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download