i want coding for upload function...actually, i want to make upload function in my system....who can help me.....plzzz anyone give me the code for upload file into system...thanks

Dani AI

Generated

provided a form and a handler that fails to save uploads. Common root causes are disabled short PHP tags, server-level upload limits or open_basedir restrictions, a non-writable destination path, or treating the upload array incorrectly. was right to point at the destination path and syntax; the checklist below covers those items plus security and debugging steps that are not present in the thread.

  • Use the full PHP opening tag <?php (short tags may be disabled).
  • Verify php.ini: file_uploads enabled, upload_max_filesize and post_max_size large enough for the file, and max_input_time/max_execution_time not timing out.
  • Avoid hard-coded htdocs paths. Create an uploads folder inside the project (use __DIR__) and ensure the webserver user owns the folder. Do not use 0777 in production.
  • Check the upload error code and $_FILES contents for diagnostics (log or var_dump when debugging).
  • Use is_uploaded_file() + move_uploaded_file() to move the file, not a plain copy(). Do not suppress errors with @.
  • Sanitize the original filename, use a unique name, verify MIME with finfo or getimagesize for images, and store uploads outside the webroot or block execution with server rules.

A minimal, safe pattern (adapt as needed):

<?php
if (!empty($_FILES['img1']) && $_FILES['img1']['error'] === UPLOAD_ERR_OK) {
    $tmp = $_FILES['img1']['tmp_name'];
    $safe = preg_replace('/[^A-Za-z0-9._-]/', '_', basename($_FILES['img1']['name']));
    $dir = __DIR__ . '/uploads';
    if (!is_dir($dir)) mkdir($dir, 0755, true);
    $target = $dir . '/' . time() . '_' . $safe;
    if (is_uploaded_file($tmp) && move_uploaded_file($tmp, $target)) {
        echo "Stored: $target";
    } else {
        error_log('Upload move failed');
    }
} else {
    error_log('Upload error: ' . ($_FILES['img1']['error'] ?? 'none'));
}
?>

See the PHP manual for handling file uploads and move_uploaded_file for full details: Handling file uploads and move_uploaded_file.

Recommended Answers

All 2 Replies

actually, this is my coding....but i can't upload file on my system......here's i attached together my html file and php file.....anyone can help me???i real need yourhelp guyz...

html file

<html>
<head>
<title>upload a file </title>
</head>
<body>
<h1> upload a file</h1>

<form method = "post" action = "do_upload.php" enctype = "multipart/form-data">

<p><strong> file to upload:</strong><br>
<input type = "file" name = "img1" size = "30" ></p>

<p> <input type = "submit" name = "submit" value = "upload file"></p>
</form>
</body>
</html>

php ffile

<?

if ($_FILES['img1'] != "") {

	@copy($_FILES[img1][tmp_name],"/Program Files/Apache Group/Apache/htdocs/".$_FILES[img1][name])
	or die ("Couldn't copy the file.");

}else{
	die ("No input file specified");
}
?>


<html>
<head>
<title> succesful file upload</title>
</head>
<body>
<h1>success!</h1>

<p>
you sent : <? echo $_FILES[img1] [name]; ?>
a <? echo $_FILES [img1] [size]; ?> byte file with
a mime type of <? echo $_FILES [img1] [type]; ?>.</p>

</body>
</html>

What's the error ? Check if the path you have specified in copy is reachable. Also, there are some mistakes.
Its always better to use $_FILES instead of
$_FILES[img1][tmp_name]. Don't give a space between $_FILES and .

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.