Hi,

I am following the Head First book on PHP/MySQL and ran into a question on setting up folders for uploading images to the web to be used in a database application. The book states, "Use an FTP program to access the file system of your web site and create the images folder beneath the web folder of the application.

  1. Why do I need to use FTP when I can just go into the control panel and create the images folder and upload my images using the file upload process?
  2. The files currently upload to the public_html folder. So when the book says create the images folder beneath the web folder does that mean as a subdirectory inside the public_html folder or inside the www folder?
  3. Or does it mean I just create the images folder at the same level?

Thanks in advance for your kind help!
Shanna

Dani AI

Generated

Brief, practical guidance to add to this thread (building on ’s helpful points and ’s Head First exercise).

Create a single, well-named folder for uploaded images and keep all image metadata (filename, uploader, timestamp, caption) in the database rather than storing raw image binaries there. Hosts call the document root different names (public_html, www, htdocs, httpdocs), so confirm which folder is the site root in the control panel. For quick one-off uploads the control panel file manager is fine; for bulk work or permission tweaks, an FTP/SFTP client is more efficient.

For better security, place uploads outside the web-accessible folder and serve them through a small script that validates access and content-type. Example (serve from outside web root):

<?php
// safe-serve.php?file=abc123.jpg
$base = '/home/ACCOUNT/uploads/';
$file = basename($_GET['file']);           // prevents directory traversal
$path = $base . $file;
if (!is_file($path)) { http_response_code(404); exit; }
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('Content-Type: ' . finfo_file($finfo, $path));
readfile($path);
?>

Server-side upload handling checklist:

  • Validate the actual file is an image (e.g., getimagesize() or Fileinfo), and enforce size limits from php.ini.
  • Rename files to a generated unique name (hash or random token) and store the original name in the DB.
  • Move uploads with move_uploaded_file() and ensure the upload directory is writable by the web process but not world-writable.
  • Disable directory listing and block script execution in the upload folder (use .htaccess rules on Apache).

Final notes: prefer SFTP/FTPS over plain FTP, test paths after uploading (relative vs absolute URLs), and if permission/ownership problems appear on shared hosting, ask the host to resolve them.

Recommended Answers

All 2 Replies

Hi,

Ans 1. FTP is a lot faster than your conventional file management in cpanel. It will be a lot easier to change file and directory permission using FTP program e.g. filezilla.

Ans 2. Yes, anything that refers to instruction such as create a new directory in your root directory.. will be beneath the public_html directory (please read extra info. below). The reason is that above public_html are not viewable to the browser. However, there are cases where some scripts e.g. php, perl can be placed above the public_html, but that out of the scope of your question.

Ans3. The image directory should be created inside the public_html directory.. Normally, your hosting provider will tell you where to put all of your files, but by default it is the public_html.

Extra info.

file permission. sometimes, books will instruct you to give permission to files or directory. To change or set permission to specific directory or files using FTP, just right click on the directory or file, and then select file permission.. type in the number suggested by the author.. e.g. 0777, 777, 0755, 755. again it all depends on server settings e.g apache module, cgi or fast cgi. Most servers, will assign persmission to a newly created directory with 755.. again it all depends in the server configuration.. e.g apache module or fast cgi?

In environment like shared hosting account, you would notice that you can easily add new domains under one account. So, the files inside the public_html belongs to the domain used when you signed up for your hosting account. The additional domains under this account will have their own directories within the public_html directory e.g seconDomainDotCom.

If yo will be working on those added domains or sub-domains, the root specific to each domain is the directory of that domain.. While, the root of your main account is the public_html

Public_html
-->this area here is the root of main account.
if addon domain is enabled, you can also find someDomainDotCom here..
-->DomainTwo
----> this is root of domain two
-->Domain Three
----->This is root of domain three.

commented: well done! +4

Great!! Thanks super answer. I appreciate the quick response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.