Question: In this assignment, you are to create a PHP file that: is named index.php reads the content of the images directory prints an tag for
In this assignment, you are to create a PHP file that:
is named index.php
reads the content of the "images" directory
prints an tag for each of the .jpg, .jpeg, .png, and .gif images in that directory
http://php.net/manual/en/function.pathinfo.php (Links to an external site.)Links to an external site.
and if the file is a .png, it will create a new directory inside the "images" directory, named "PNG", and copy the file into the new directory
http://php.net/manual/en/function.copy.php (Links to an external site.)Links to an external site.
Since you are creating a directory inside of a directory, your code must check whether the current file is a directory, if it is, then you should skip that file
For example, take a look at the attached screenshot. In the screenshot, you are looking at a folder named Challenge8. I've placed that folder within my /var/www/html directory (public html directory) on my Amazon EC2 instance. It contains an index.php script and many different images (6 jpeg images, 4 png images, and 5 gif images, total 15 images) inside a folder named "images". Those images are also attached as images.zip. You may use them or pick your own.
When you navigate to index.php in a web browser, it will use PHP to look at the names of the files inside of Challenge8/images directory. If any of those files ends with a ".jpg", ".jpeg", ".png", or ".gif" extension, index.php will print an tag for it. Then if the image is a ".png" it will create a new directory called "PNG", if one does not already exist, and copy that image into the new directory.
This result is this: http://ec2-34-226-140-164.compute-1.amazonaws.com/PublicChallenges/Challenge8/ (Links to an external site.)Links to an external site.
Make sure to take a look at my source code with your developer tools (Right click on the page and click "View Page Source"). Note that the tags are nested within a properly formatted HTML5 document with the 5 required tags and the requirements to pass the validation at https://validator.w3.org/ (Links to an external site.)Links to an external site. (2 additional items). To make each image on a new line, since images are inline elements, I placed div tags around the image tag because div is a block element.
Last but not least, make sure you refer to documentation. You will need to use resources like Google and http://php.net/ (Links to an external site.)Links to an external site. to solve this challenge. You will need to call PHP functions for directory operations. You will also need to use the PHP documentation to figure out how to get the extension of a file. Lastly, you will need to figure out how to copy files using the PHP copy() function http://php.net/manual/en/function.copy.php (Links to an external site.)Links to an external site. or using PHP Input/Output (IO) reading a file and writting the file to the new destination. (Can also use the way were you read all the lines in the file and write it to a new file).
Requirements:
Page must have the 5 required tags
Page should include the 2 extra items to pass validation
If you compare strings then you should use a binary safe string comparison, e.g. strcmp()
Some methods do not require a binary safe string comparison
All images should have a height of 500px and a width of 800px
Code must check whether directory exists, if not print an error statement
Code must open the directory and close the directory properly, if can't open directory print an error statement
Code must create the new directory "PNG", if it does not exists (It should not be created the first time you run the code, and for testing you will want to make sure you delete the "PNG" folder each time to make sure your code works properly)For extra credit, you can create "PNG" and all it's parent directories, if they do not exists (5 bonus points)
http://php.net/manual/en/function.mkdir.php (Links to an external site.)Links to an external site.
Then the code must take the file with .png extension and copy them into the new directory called "PNG" inside the "Images" directory In order to copy the files over to the PNG folder, you will need to make sure you have the correct permissions. Copying files from the browser means the folder needs to be open for read, write, and execute to the world. There are two ways to accomplish this, one would be to set the permissions when you are creating the PNG folder from the code using the $mode parameter in the mkdir() function. Another way to change permissions would be to call the chmod() function. Both the documentations are below:
http://php.net/manual/en/function.mkdir.php (Links to an external site.)Links to an external site.
http://php.net/manual/en/function.chmod.php (Links to an external site.)Links to an external site.
Since we introduced a directory into the "Images" directory, the code must check whether the current file is a directory when looping through the "images" directory, if the file is a directory, then the code should skip the current file and continue to the next one
http://php.net/manual/en/control-structures.continue.php (Links to an external site.)Links to an external site.
In the end, the .png files will be in the images directory and the PNG directory
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
