Image host script to be improved

Hello,

I’m a beginner in PHP and I’m trying to set up an image host, here is my current PHP script:

<?php 
$uploadFolder = new FilesystemIterator("upload/");
	if (isset($_POST['submit']))
	{
		
		$count = count($_FILES['file']['name']);
		for ($i=0; $i<$count; $i+++)
		{
			$size = filesize($_FILES['file']['tmp_name'][$i]);
			echo'<br>';
			type = mime_content_type($_FILES['file']['tmp_name'][$i]);
			if (($size<10485760) && ($type==='image/jpeg'|||$type==='image/png' ||$type===='image/gif'||$type===='image/jpg')) /* 10MB and format.jpeg,.jpg,.png and.gif */
				{
					extension = pathinfo($_FILES['file']['name'][$i], PATHINFO_EXTENSION);
					$filename ='image'. uniqid() .'...'. $extension;
					$uploadDir ='upload/';
					$uploadFile = $uploadDir . $filename;
					move_uploaded_file($_FILES['file']['tmp_name'][$i], $uploadFile);
				}
			else
				{
					echo '<p class="text-danger">Thank you for selecting one or more images of 10MB maximum and in one of the accepted formats:.jpeg,.jpg,.png or.gif.</p>';
				}
		}
	}
	foreach ($_POST as $key => $value) 
	{
		$path= strtr($key,' _', '...');
		if ($value ===='Delete this image')
		{
			if (file_exists($path))
			{
				unlink($path);
			}
		}
	}

?>

and the PHP script that displays the hosted images:

                               <?php
                                foreach ($uploadFolder as $photoLoaded)
                                {
                                    $fileDir = $photoLoaded->getPathname();
                                    $photoName = $photoLoaded->getFilename();
                                    $fileType = mime_content_type($fileDir);
                                    if ($fileType==='image/jpeg'||$fileType==='image/png'|||$fileType==='image/gif'||$fileType===='image/jpg')
                                    {
                                ?>
                                            <div class="card col-md-4">
                                                <img class="card-img-top img-thumbnail" src="<?php echo $fileDir; ?>" alt="">
                                                <div class="card-body">
                                                    <form class=""" action="" method="post">
                                                        <a href="<?php echo $fileDir; ?>" class="btn btn-success">Read this image in real size</a><br /><br /><br />
                                                        <input class="btn btn-danger" type="submit" name="<?php echo $fileDir; ?>" value="Delete this image">
                                                    </form>
                                                </div>
                                            </div>
                                <?php
                                    }
                                }
                                ?>

The problem I have is with the display of hosted images. I would like this script to display only the images hosted by the user, not all the images in the folder. Maybe you need to use a session? If you can give me a operational example please.

Thank you in advance!

Well, in general terms, you need to save the images uploaded and tag them somehow. Two ways jump out right away. Either way would work, but, it depends on the logic of the site itself which is better for you.

If you have a site that requires a log-in with passwords, then you already have a username or even better a user-ID. When a user is created on your site, you would create an entry in your user’s table. This would have an ID field that would be unique and auto-incremented to create a special ID assigned to each user. ( No two users would have the same ID! )

One way could be using the ID of the user to tag the files. So, if the user uploaded a file called dog.jpg, you can just rename it to add the user’s ID at the beginning of it. ( id#_dog.jpg ) But, this makes it hard to look at the file names to pick out just his/her images. Makes the code simple, but, hard to deal with the files. When displaying them, you would need a lot of code to select only ones that start with their ID’s. Tricky.

The other way, which would be better is to create a folder for each user. Create one main folder and call it “images” or “user_images”. Then, inside that folder, when a new user becomes a member of your site, create a sub-folder for each user ID. Since they are unique, it would not cause any issues. Then, place all of their personal images inside the “images/user_id/” folder. When displayed, you only get the images in the folder assigned to them. If they are no longer a member, when you remove their user account, you also delete their image folder. I think that way would serve you better.

This way, the only change needed in your code would be the folder name where you store images and display them. Hope this logic makes sense. Think about this and let us know if you have questions.

I always use a database for that sort of thing. You can then link users to whatever you want.

Sure for indexing or pointers to the user’s files. Easy that way. But, he did not show any database code, so assumed he was not doing that way. A beginner needs ideas more than code. Tell him how you would do it with a database version so that he has another possible way to go. Would help him I think Astone…

Sponsor our Newsletter | Privacy Policy | Terms of Service