hi everyone,
i have a problem in my php code.i hope anyone of you can help me in this problem.
i have a problem for image uploading in particular folder.
my code works but this will upload images in root directory called as 'upimg'.
i want to upload images into particular directory(variable name as $dirna).
i m giving you my code.so pls help me.

<?php
if(isset($_SESSION['alnamae']))
{
	$alaname = $_SESSION['alnamae'];
}
else
{
	header("Location: addimg.php");
}
if(isset($_SESSION['dirna']))
{
	$dirna = $_SESSION['dirna'];
	//echo $dirna;
}
else
{
header("Location: addimg.php");
}
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>

<head><title>Multiple image upload</title>

</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?php
error_reporting(0);
	$path = "upimg/" . $dirna . "/";
	echo $path;
while(list($key,$value) = each($_FILES['images']['name']))
		{
			if(!empty($value))
			{
				$filename = $value;
					$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line

					$add = $path."$filename";
					//$add = "upimg/$filename";
                       //echo $_FILES['images']['type'][$key];
			     // echo "<br>";
					copy($_FILES['images']['tmp_name'][$key], $add);
					//chmod("$add",0777);
			

			}
		}


?>
 
</body>

</html>

Dani AI

Generated

correctly identified the immediate cause: session-held directory information was empty because the session was not started. 's reminder about permissions is also valid — the upload target must exist and be writable by the webserver. The following adds practical, long-term safeguards and a compact, safer upload pattern to prevent similar problems.

Common pitfalls and quick checks:

  • Confirm the HTML form uses enctype="multipart/form-data" and the file input is named as an array (images[]) when uploading multiple files.
  • Avoid each() (deprecated) and copy() for uploaded files; prefer foreach and move_uploaded_file() so PHP verifies the file is a genuine upload.
  • Sanitize dirna (restrict to a whitelist pattern like letters, numbers, _ and -) to prevent path traversal.
  • Create the target directory if missing with mkdir(..., 0755, true) and verify is_writable() rather than setting 0777; use 0644 for files.
  • Always check $_FILES['images']['error'][$i] and validate content with getimagesize() or finfo_file() before saving.
  • Check php.ini limits (upload_max_filesize, post_max_size, max_file_uploads) if uploads silently fail.

Example pattern (concise, modern):

$files = $_FILES['images'];
$target = __DIR__.'/upimg/'.preg_replace('/[^A-Za-z0-9_-]/','',$dirna);
if (!is_dir($target)) mkdir($target,0755,true);
foreach ($files['name'] as $i => $name) {
    if ($files['error'][$i] !== UPLOAD_ERR_OK) continue;
    $tmp = $files['tmp_name'][$i];
    if (getimagesize($tmp) === false) continue; // not an image
    $safe = uniqid('',true).'_'.preg_replace('/[^A-Za-z0-9._-]/','_',$name);
    move_uploaded_file($tmp, $target.'/'.$safe);
    chmod($target.'/'.$safe, 0644);
}

Troubleshooting notes: enable error reporting during development, inspect var_dump($_FILES), check ownership (webserver user) and server logs. Avoid error_reporting(0) while debugging.

Recommended Answers

All 3 Replies

Hello Ujjval
I think you should check for the permission to the directory called by '$dirna'.
use chmod to check $dirna. You should check $path = "upimg/".$dirna;
chmod ($path,0777);
then use $add=$path."/".$filename
Hope it will work.

You need to check whether any value is assigned to a varible $dirna or not. Though you are setting value to $dirna through session, you need to start session in top of this page

session_start()

hope this helps.

commented: because he solved my question. +1

hey paulrajj thanks for your golden help.
this was a really silly mistake.
now my question is solved.
thanks again

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.