I am using php 5.3 on client machine. while xammp is installed on the server of practical lab of our college. I wanted to upload a file and then want to move it in specific folder. So code of uploading file is working properly and uploaded successfully on our server. But now i want to move that file in the folder(where i careated php project and form) which i created on my client pc. How can i do it? Here is the code which i tried but it just upload file to server but cannot move.
Here is php code
<?php
if(isset($_POST['submit']))
{
$fname=$_FILES['file']['name'];
if($_FILES['file']['name']!="")
{
move_uploaded_file($_FILES["file"]["tmp_name"],"/images".$_FILES["file"]["name"]);
echo "uploaded";
}
else
{
echo "Not Uploaded";
}
}
?>
HTML code
<form method="post" name="frm1" enctype="multipart/form-data">
<label for="fileSelect">Filename:</label>
<input type="file" name="file" />
<input type="submit" name="submit" value="Upload">
</form>
After selecting and submitting the file it shows the output "uploaded". But does not display in the images folder which i created in the project.
Please try to change:
move_uploaded_file($_FILES["file"]["tmp_name"],"/images/".$_FILES["file"]["name"]);
Maybe you have missed "/" between dir and name.
Reference link : http://php.net/manual/en/function.move-uploaded-file.php
My hunch is that the target directory is not within the root structure for your site so the root relative path
you have assigned /images
is not available. If that is the case use a full path string to that directory as below:
<?php
/* utility to recursively create folder path if needed */
function createpath( $path=NULL, $perm=0644 ) {
if( !file_exists( $path ) ) {
createpath( dirname( $path ) );
mkdir( $path, $perm, TRUE );
clearstatcache();
}
}
if( isset( $_POST['submit'] ) && !empty( $_FILES['file'] ) ) {
$status = false;
$writeenable=true;
$name = $_FILES['file']['name'];
$error = $_FILES['file']['error'];
$tmp = $_FILES['file']['tmp_name'];
/* If the directory is in the DOCUMENT_ROOT structure */
/*
$targetdir = $_SERVER['DOCUMENT_ROOT'] . '/images';
if( !file_exists( $targetdir ) )createpath( $targetdir );
*/
/* If the directory is outside of the DOCUMENT_ROOT */
$targetdir = 'D:\Sem-2\PHP\Unit-3\file-upload\images';
if( !file_exists( $targetdir ) ) exit('Warning: Target directory cannot be found');
if( !is_writable( $targetdir ) ) {
if( $writeenable ) chmod( $targetdir,0644 );
else exit('unable to write to target directory');
}
if( $error == UPLOAD_ERR_OK && is_uploaded_file( $tmp ) ){
$status = move_uploaded_file( $tmp , $targetdir . '/' . $name );
}
echo $status ? 'uploaded' : 'Not Uploaded';
}
?>
Try this:
$fileTmpName = $_FILES['file']['tmp_name']; //Temporary file location
$fileDestination ='./uploadScript'; // put a dot '.' before the slash '/'
move_uploaded_file ($fileTmpName, $fileDestination); // move file FROM '$fileTmpName' TO '$fileDestination'
Reference: https://www.php.net/manual/en/function.move-uploaded-file.php
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.