i am creating file upload script and saving the file path to the sql, i use this script
$location = 'files/' . $file_name;
$full_path = realpath($location);
It return
D:xampphtdocsau
why theres no slash, am i doing it wrong
Related
I am using file put contents and set a file name but file saved on the directory where the script was written. How to do to set the full path to save to disc or desktop. Something like browse window and save path.
$fileName = 'etykieta_' . $package_no . '.' . $xml->label->format;
file_put_contents($fileName, base64_decode($xml->label->base64));
And i want to set a file path in a window.
Ι have somewhere located in my pc a txt file, let's say for example C:\Users\my_file.txt. Is there a way to print its size? As I search it, I am given:
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
But what path should I give to filename variable?
In Linux, the path separator is /. In Windows, it is either \, / or \\.
I personally use front-slashes / on both OS so I never get confused.
Below you can find 3 valid full paths on Windows, i.e.:
C:/Users/my_file.txt
or
C:\\Users\\my_file.txt
or simply:
C:\Users\my_file.txt
To get the filesize you can use the function filesize
$myFile = "C:\Users\my_file.txt"
echo filesize($myFile) . "bytes";
If the file is on the same dir as the php script you can use a relative path:
$myFile = "my_file.txt"
echo filesize($myFile) . "bytes";
If the file is one dir below you can also use a relative path.
$myFile = "../my_file.txt"
echo filesize($myFile) . "bytes";
Another relative path example, this time 1 dir UP:
$myFile = "./newdir/my_file.txt"
echo filesize($myFile) . "bytes";
Ideally you just give filesize an absolute path.
If that is not an option, you have to give it a relative path (relative to the PHP-file you are in).
Say you have a dir files with another folder in it (php), and in it again there is a file called filesize.php. In the files folder you have a file called bigfile.txt.
To get the filesize of the bigfile.txt in your php script which is in php/filesize.php you could do one of the following:
<?php
$filesize = filesize("../bigfile.txt"); // One folder up (relative)
$filesize = filesize("C:\files\bigfile.txt"); // Absolute path
I am uploading a file using PHP to a folder in my directory and am unable to rename it using the following code
$da = date("dmY");
$ja = $uid.$da;
$mukesh = $app.$ja;
// If no errors, upload the image, else, output the errors
if($err == '') {
if(move_uploaded_file($_FILES['userfile'][$mukesh], $uploadpath));
Here's PHP's official document about how to handle uploads: http://www.php.net/manual/en/features.file-upload.post-method.php
The method move_uploaded_file() requires two parameters, a filename of the temp file, and a new location.
$tmp = $_FILES['userfile']['tmp_name']; // temp path
move_uploaded_file($tmp, $uploadpath . '/' . $mukesh);
You will need to name your input element userfile.
<input type="file" name="userfile" />
Based on code snippet provided, you can do following
move_uploaded_file ($_FILES["userfile"]["tmp_name"], $uploadpath);
When you upload a file, files will be store in upload location specified in php.ini using a temporary name. This file location with name can be accessed by $_FILES["userfile"]["tmp_name"]
Lets say you upload an image.if no error then
$uploads_dir = 'as per you defined';
$tmp_name = $_FILES["userfile"]["tmp_name"];
$name = 'custom_file_name.png';//$_FILES["userfile"]["name"];
move_uploaded_file($tmp_name, $uploads_dir."/".$name);
You are renaming the temp name of file ...
When you want to rename the change the name with which you want to store the file
$filename = time().$_FILES['userfile']['name'];
$upload_path = 'path_to_ur_upload_folder'.$filename;
move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path );
first param in move_upload_file is temporary name that will be used by stream while copying an d uploading .. the second parameter is path where your file will be saved (along with file name).. it is the second parameter which will help you in renaming of file which is being uploaded
I am looking to save an xml file to a different directory from the root using php5. any ideas?
//write to the file
$filename = $id . ".xml";
$doc->save($filename);
I want to save the file to the /xml/ directory.
Change the argument to $doc->save to include the path
$filename = '/xml/' . $id . ".xml";
$doc->save($filename);
Now the thing to bear in mind is that this is a filesystem path, not web URL so its literally going to save in /xml not DOCUMENT_ROOT/xml.
What is this code doing?
<?php
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
}
?>
It is basically uploading a file and echoing the target file's name.
There should also be something like an HTML form to send the file to this script.
It accepts an user uploaded file, and puts it in your webroot in a folder, specified by the user. Then it outputs the path of the uploaded file.
This code is to upload a file in the destination directory. Is there anything else do you want to understand.
This will upload the file in the path you have in variable $_REQUEST['folder']
Storing files uploaded (possibly via HTML form) onto server.
It's accepting a file posted by a HTML form and uploading it to a certain directory within a server. After that it's displaying the location of the file on screen (in the browser).