Hi everyone, looking for some advice please.

I had a mkdir working great on a shared hosting account I had,
I have since moved from a shared hosting account to a virtual dedicated server, now the mkdir is not working as previous.

Before i moved server is was using a simple

//make directory using the users id and the last id
mkdir("products/$id/$lastid",0777);

I have tried many different ways trying to get the above to work like it did on the shared hosting.

All the information is being sent to mysql correctly, but no directory is being created in the users folder.

Can someone point me in the right direction please.
here is the code im using on the VDS -

$dest = "/var/www/vhosts/domain/httpdocs/products/$id/";
$user_name = "ftp_username";
mkdir("$lastid",0777);
chown($dest, $user_name);

////////////////////////////////////////////////////////////////////////////////////////////////////////////
				$img1 = $_FILES['jan_image']['name'];
				//add a security check, to make sure only allowed files are uploaded
				$ext=explode('.',$img1);//explode the image name into an array to get the extension
				$allowed_exts=array('jpg','jpeg','png','bmp','gif');// allowed extensions, add or remove any extension you want allowed(LOWER CASE ONLY)
				if(in_array(strtolower($ext[1]), $allowed_exts)){//file type is allowed!
				//trades/$id/$lastid
					$dest="$lastid/".$img1;
					$val= move_uploaded_file($_FILES['jan_image']['tmp_name'], $dest);
				
					chmod($dest, 0644);//make sure you have permissions for the file
					if($val)
					{ //update database and rename
					rename($dest, ''.$lastid.'/logo.jpg');
					$dest="".$lastid."/logo.jpg";//set $dest to the new file name so the database entry is correct
						
					mysql_query("update table set img1='$dest' where uid=$lastid");
					}
					}

what I am not sure how to do, if required is to create a script to add the ftp-username and password, if I cant get the above to like on the shared hosting server.

Dani AI

Generated

Quick summary for : the move from shared hosting to a VDS usually changes two things that break uploads — the path you’re creating and the filesystem ownership/permissions. is on the right track (parent directories must exist or be created), but the real problems are often (a) creating a directory with a bare name so it lands in PHP’s current working directory instead of under your intended /var/www path, and (b) PHP not having the UID/GID needed to own or write into that path. Calling chown() from PHP rarely helps unless the PHP process runs as root.

Quick tests to run inside the site (drop this file alongside your script to get clear feedback):

<?php
echo "cwd: ".getcwd()."\n";
$test = __DIR__ . '/write-test.txt';
if (@file_put_contents($test, "x") === false) {
    var_dump(error_get_last());
} else {
    unlink($test);
    echo "write OK\n";
}
?>

If the script can’t write the test file, move_uploaded_file() will also fail — always check its boolean return and log error_get_last().

From the server shell, confirm ownership and permissions (replace path as needed):

ls -ld /var/www/vhosts/domain/httpdocs/products
stat /var/www/vhosts/domain/httpdocs/products
whoami   # run under the web/PHP user to see which account owns the running process

If the directory is owned by root or another user, fix ownership from SSH (not via PHP): sudo chown -R yourwebuser:yourgroup /var/www/vhosts/domain/httpdocs/products and avoid using 0777; prefer 0755 or group-write plus ACLs (setfacl) for finer control.

Action checklist

  1. Verify you’re building and passing absolute paths (use __DIR__ or getcwd() to confirm).
  2. Test writing with file_put_contents and inspect error_get_last().
  3. Inspect and correct owner/group from SSH (use chown or setfacl).
  4. Check PHP error logs for permission errors.
  5. Do not store FTP credentials in scripts — fix filesystem permissions or run PHP-FPM as the site user instead.

These steps will usually reveal whether the issue is a path, a permission, or an ownership problem and point to the correct fix.

Try the following:

mkdir("products/$id/$lastid",0777,true);
mkdir("products",0777);
mkdir("products/$id",0777);
mkdir("products/$id/$lastid",0777);
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.