I never uploaded a file before and I'm finding some problems regarding uploading a file in a directory which have some security. I searched on the internet and I found out that I need to make use of chmod php function however I have been fighting against my computer for two days now and I can't understand what is wrong with my coding. So here is my coding:

chmod("directory",0777);
if(isset($_POST['submit']))
{
	
	$target_path = "directory";
	$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
			
			
	if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
	{
				
				
		mysql_query("UPDATE account SET image = 'name' WHERE id = '$id'");
		echo"$name";
	} else{
		echo"NO";
	}
}

Thank you for your attention and time

Dani AI

Generated

reported that move_uploaded_file is always returning false; ’s link is a decent primer, but the thread is missing focused debugging steps. Common culprits are: form not using enctype="multipart/form-data", wrong input name, PHP upload limits (upload_max_filesize / post_max_size), $_FILES showing an error code, using a relative path that doesn’t exist, directory ownership/permissions problems, open_basedir/SElinux restrictions, or trying to rely on chmod() from PHP on a server where the script user isn’t the owner.

Quick, practical checklist to work through next:

  • Confirm the HTML form uses method="post" and enctype="multipart/form-data".
  • var_dump($_FILES) to see the error code and tmp_name. Interpret $_FILES[...]['error'] (UPLOADERR* constants).
  • Use an absolute path for the destination (e.g. __DIR__ . '/uploads/') and make sure the directory exists.
  • Ensure the webserver user can write to that directory (ownership or proper permissions). Avoid 0777 as a first fix — it’s insecure.
  • Check PHP limits (phpinfo/ini_get for upload_max_filesize, post_max_size) and server logs for clues.
  • If on SELinux-enabled host, check context; on Windows chmod() behaves differently.

A minimal robust pattern (adapt names to your form) to follow:

$fileKey = 'file_field';
$uploadDir = __DIR__ . '/uploads/';

if (!is_dir($uploadDir) || !is_writable($uploadDir)) {
    error_log("Upload dir not writable: $uploadDir");
}

if (!empty($_FILES[$fileKey]) && $_FILES[$fileKey]['error'] === UPLOAD_ERR_OK) {
    $tmp  = $_FILES[$fileKey]['tmp_name'];
    $name = basename($_FILES[$fileKey]['name']);
    $dest = $uploadDir . $name;
    if (is_uploaded_file($tmp) && move_uploaded_file($tmp, $dest)) {
        chmod($dest, 0644);
    } else {
        error_log('move_uploaded_file failed: '. print_r($_FILES[$fileKey], true));
    }
} else {
    error_log('Upload error: '. (isset($_FILES[$fileKey]['error']) ? $_FILES[$fileKey]['error'] : 'no file'));
}

Security and production notes: always validate file type/size, sanitize or replace filenames (use a random name or hash), prefer storing uploaded files outside the webroot or preventing execution, and use prepared statements when saving paths to the database. If chmod() or ownership fixes are needed, set permissions from the server shell or hosting control panel rather than relying on PHP on many shared hosts.

So whats the problem ???is any error notified???or the uploading is not working....
Specify so that i could help you...

There is no error.

(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)

is always false

Check this link to get more information about your problem's solution...
Click here

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.