Jump to content

Few PHP questions (folders and files)


Sunamena

Recommended Posts

Hello everyone i have a few PHP questions.


First one:

How can I say that for each image in a specified folder, it has to place that image?
I have 10 images in the gallery folder (tomorow it can be 12). How do i tell my code to place an <img src="filename"> for each image?


Second question: 

When displaying something that a user posted (example: guestbook). How can i allow ENTERS to be displayed when getting the data from the database? It always skips the enters.



Third question: 

How exactly can i allow users to upload a file to a certain folder (must be a jpg file). The name of the image will be modified, and it should be resized to 200 kilobyte.

 


Fourth question:


Can i change certain parts of the text i get from the database (example: ":)" and change it to something differently? Say: "<img src="smily.png>"?



Thanks in advance for the answers =D
 

Edited by Sunamena
Link to comment
Share on other sites

You can use scandir() to list files in a directory and generate the HTML for the images.

The line breaks exist, but HTML doesn't render line breaks unless you specifically put a <br> element. You can use nl2br() to show line breaks in the text provided by users.

You can't choose the size of a file, but you can give a size limit in the file uploader. There are plenty of PHP file upload tutorials, you should look at some of them. The W3Schools tutorial has sections explaining how to limit the file size and file type.

You can use str_replace() to change substrings into something else, such as in the following example:

<?php
$string = str_replace(':)', '<img src="smiley.gif">', $string);
?>

 

  • Like 1
Link to comment
Share on other sites

I'm not a PHP expert but for your first question, I would recommend you to use the FilesystemIterator class (implemented in PHP ≥ 5.3). I've just finished doing an image gallery for my project and this is the easiest, most straigth-forward approach I've found.

Here is an example on how to use it:

$dir = '/yourDirectory';
$fileDir = new FilesystemIterator($dir); // creates a new instance of the class as $fileDir
foreach ($fileDir as $file) {
	if ($file->getExtension() == 'jpg') {  // lists all .jpg in $dir
    		echo '<img src="'.$file.'"/>
        	';
	}
}

Hope that helps.

Link to comment
Share on other sites

First question, something like this should work:

I made this with my own paths so you can understand what is the difference between $dir and $path_images.

If you don't use the if condition then your scandir will show other files that will corrupt the img tag.

$dir = '/xampp/htdocs/test/uploads';
$path_images = 'uploads/';
$files = scandir($dir);

foreach($files as $key)
{
	if(!preg_match('/.jpg|.jpeg/', $key)) continue;
	echo '<img src="' . $path_images . $key . '">';
}

Second question

As @Ingolme said you can use n2lbar() for that.

Let's take this script as an example:

//First we need to access data
try {
	$sql = "SELECT user_comment FROM comments";
	$stmt = $db->prepare($sql);
	$stmt->execute();
}
catch (PDOException $e)
{
	echo "Error: " . $e->getMessage();
}

//Now fetch it and display it
while ($row = $stmt->fetch())
{
	echo nl2br($row['user_comment']);
	//This will display row breaks
}

Third question

I don't know to properly resize a picture with php, i think it's not that easy to do that, but, you can just add a if condition so that the user can upload only jpeg file about 200kb, this way will cut a lot of your work to code.

And what do you mean by a specific directory? You want like an user to have it's own directory?

Fourth question

The user @Ingolme explained it pretty well with str_replace(); .

Edited by Gabrielphp
Link to comment
Share on other sites

How are you using your images?  What is the purpose of their display?  Do you understand Javascript and jQuery.  I have found Colorbox (a jQuery plug-in) to be very useful.  It is client-side.

Roddy

p.s. No offense implied to the server-side PHP forum! :-)

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...