Search
 
SCRIPT & CODE EXAMPLE
 

PHP

basic code for file upload in php

//This is the minimal code for an image upload for first time learners
//html portion
<!DOCTYPE html>
<html>
<head>
	<title>ImageUpload</title>
</head>
<body>
	<form action="upload.php" method="post" enctype="multipart/form-data">
		<label>Username</label>
		<input type="text" name="username">
		<br>
		<label>UploadImage</label>
		<input type="file" name='myfile'>
		<br/>
		<input type="submit" value="upload">
	</form>
</body>
</html>
  
 //php portion
  <?php
	$user=$_POST['username'];
	$image=$_FILES['myfile'];
	echo "Hello $user <br/>";
	echo "File Name<b>::</b> ".$image['name'];

	move_uploaded_file($image['tmp_name'],"photos/".$image['name']);
	//here the "photos" folder is in same folder as the upload.php, 
	//otherwise complete url has to be mentioned
	?>
Comment

Upload file in php

<?php
// connect to the database
$conn = mysqli_connect('localhost', 'root', '', 'file-management');

// Uploads files
if (isset($_POST['save'])) { // if save button on the form is clicked
    // name of the uploaded file
    $filename = $_FILES['myfile']['name'];

    // destination of the file on the server
    $destination = 'uploads/' . $filename;

    // get the file extension
    $extension = pathinfo($filename, PATHINFO_EXTENSION);

    // the physical file on a temporary uploads directory on the server
    $file = $_FILES['myfile']['tmp_name'];
    $size = $_FILES['myfile']['size'];

    if (!in_array($extension, ['zip', 'pdf', 'docx'])) {
        echo "You file extension must be .zip, .pdf or .docx";
    } elseif ($_FILES['myfile']['size'] > 1000000) { // file shouldn't be larger than 1Megabyte
        echo "File too large!";
    } else {
        // move the uploaded (temporary) file to the specified destination
        if (move_uploaded_file($file, $destination)) {
            $sql = "INSERT INTO files (name, size, downloads) VALUES ('$filename', $size, 0)";
            if (mysqli_query($conn, $sql)) {
                echo "File uploaded successfully";
            }
        } else {
            echo "Failed to upload file.";
        }
    }
}
Comment

php upload file

<?php
$filename = basename($_FILES['myfile']['name']);
if (move_uploaded_file($_FILES['myfile']['tmp_name'], "files/$filename")) {
  echo "File uploaded";
} else {
  echo "An error occurred";
}
?>
<form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>" method="post" enctype="multipart/form-data">
  <label>File: <input type="file" name="myfile" /></label>
    <input type="submit" value="send" />
  </form>
Comment

PHP File Upload

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
  Select image to upload:
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Php :: how to find the name of login user in laravel 
Php :: php rename files in directory 
Php :: laravel cmd command to watch logs 
Php :: php get extension from file from form submit 
Php :: in loop how add string by comma in php 
Php :: laravel query get big table records 
Php :: wordpress echo the date in post 
Php :: wordpress wp_enqueue_script footer 
Php :: pdo get row count 
Php :: php get object keys 
Php :: convert string to array laravel 
Php :: laravel array remove key 
Php :: laravel eloquent get last record 
Php :: php key value dictionary 
Php :: php multi type parameter php multi type parameter 
Php :: Invalid argument supplied for foreach() 
Php :: get order details by id woocommerce 
Php :: array_key_exists vs isset 
Php :: php hash password using bcrypt 
Php :: php find text in variable 
Php :: how match array in laravel collection 
Php :: php include files 
Php :: Could not find package laravel/ with stability stable. 
Php :: permissions on ssh 
Php :: explode in laravel blade 
Php :: php open csv 
Php :: laravel collection chunk 
Php :: carbon parse timestamp 
Php :: laravel form validation 
Php :: convert xml file to array php 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =