Tuesday, 9 June 2020

uploading files using php



uploading files using php
uplaod .php
using upload php we will upload files into upload folder
<?php
ist it checks the file is submitted, if submtted, create the variable name and assign the file name which is trying to upload, asssign the directory to newname, using if and else - if file is uploaded, using move_uploaded_ifle- move the file from temp location to original location, after succesfully uploaded, it shows uploaded, otherwise plese select a file
if(isset($_FILES['userfile']['name'])){
not post, userfile is a name value, whenerver file is uploaded
$name = $_FILES['userfile']['name'];
mutlidimensional array, and it stores the file informations, if we echo this echo $names; - it prints the file u selected and submit, using this get the file name
$tmp_name = $_FILES['userfile']['tmp_name'];
$location = 'upload/';
directory, we created the directory in manual
$new_name = $location.$name;
where we store - upload/orange.jpg(which file is uploaded)

if(!empty($name)){
if we select the file, it have to do one work
if (move_uploaded_file($tmp_name, $new_name)){
uploaded file stores from temp folder to new name
echo 'uploaded !';
}
} else (
echo 'please select  a file';
} ?>
<form action="upload.php" method="POST" enctype ="multipart/form-data">
<input type ="file" name ="userfile">
to upload we need enctype
<input type="submit" value="upload">
</form>
echo $name = $_FILES['userfile']['size'];
it prints the size of the file
echo $type = $_FILES['userfile']['type'];
what Is the type file means it is jpeg or png
$tmp_file = $_FILES['userfile']['tmp_name'];
to upload the file we need some temporary locations, whenever u upload, it sends to temporary location, from that temporary, we can move to required location
bytes

str_replace() - Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.
<?php
echo str_replace("world""Dolly""Hello world!"); // outputs Hello Dolly!
?>
how to find the base name 
<?php
$path = "/testweb/home.php";

//Show filenameecho basename($path) ."<br/>";

//Show filename, but cut off file extension for ".php" filesecho basename($path,".php");
?>


ouytput is
home.php
home


The first parameter in the HTTP context is either inline (default value, indicating it can be displayed inside the Web page, or as the Web page) or attachment (indicating it should be downloaded; most browsers presenting a 'Save as' dialog, prefilled with the value of the filename parameters if present).
Content-Disposition: inline
Content-Disposition: attachment
Content-Disposition: attachment; filename="filename.jpg"
how to write the code to download the file

 <?php

       $f="resume.doc";   

       $file = ("myfolder/$f");

       $filetype=filetype($file);

       $filename=basename($file);

       header ("Content-Type: ".$filetype);

       header ("Content-Length: ".filesize($file));

       header ("Content-Disposition: attachment; filename=".$filename);

       readfile($file);

  ?> 
Suppose You have a directory inside with collection of multiple related Files . Display all stored files using opendir( ) and readdir( ) function. Download those particular file on w
<?php

       $f="resume.doc";   
load teh file name to varaible
       $file = ("myfolder/$f");
load the oath name to a variable
       $filetype=filetype($file);
find the file type
       $filename=basename($file);
get the path name
       header ("Content-Type: ".$filetype);
tells to browser it is which file type
       header ("Content-Length: ".filesize($file));
tells to browsetr whats the file size
       header ("Content-Disposition: attachment; filename=".$filename);
tells to browser it is downloadable or display with in a web page
       readfile($file);

  ?>

PHP Download File Example: Text File
1.    <?php  
2.    $file_url = 'http://www.javatpoint.com/f.txt';  
3.    header('Content-Type: application/octet-stream');  
4.    header("Content-Transfer-Encoding: utf-8");   
5.    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");   
6.    readfile($file_url);  
7.    ?>  


PHP Download File Example: Binary File


1.    <?php  
2.    $file_url = 'http://www.myremoteserver.com/file.exe';  
3.    header('Content-Type: application/octet-stream');  
4.    header("Content-Transfer-Encoding: Binary");   
5.    header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");   
6.    readfile($file_url);  
7.    ?>  


isset
$_FILES['userfile']['name'];
concatenation
if elese
move_uploaded_file
!empty
type input
type submit input
enctype
$_FILES['userfile']['size'];
$_FILES['userfile']['type'];
 $_FILES['userfile']['tmp_name'];
str_replace
how to check file is uploaded or not
how to get the file name which is uploaded
how to create the directory using concatenation
how to chcek is really uploaded or not
how to move form temporary file to real location
after redirect, where it moves
how to uplload the file 
how to submit the file to uplaod
how to know the size of file
how to know ht type of file
how to send to tempertoy folder



No comments:

Post a Comment