Interpret Code - Non PHP Programmer - php

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).

Related

File full path location using php

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

php move_uploaded_file not creating file

I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}

PHP file upload in another directory

I am uploading files in php. The upload directory is
/var/www/html/oop/uploads/images/
But after uploading when i open the folder, it contains no image whatsoever. But when I try to include the image in a web page like this,
<img src="http://localhost/oop/uploads/images/1472722513.jpg" />
it is being included in the web page and works fine. I don't know how this is possible, I am using ubuntu 16.04.
my upload code
$file_path = '/var/www/html/oop/uploads/images/';
$name = $_SERVER['image']['name'];
$name = explode('.', $name);
$name = array_reverse($name);
$file_name = time() . '.' . $name[0];
$temporary_location = $_SERVER['image']['name']['tmp_name'];
if(move_uploaded_file($temporary_location, $file_path.$file_name)) {
echo "all ok";
} else {
echo $_SERVER['image']['name']['error'];
}
You didn't understand the concept of document root. A webserver does not reflect the local file system to public, mainly for security reasons.
When you are able to reference /var/www/../image.jpeg in your web browser, this actually points to a path relative to your document root e.g. /var/www/html/var/www/images/image.png
Read more about document root here
https://httpd.apache.org/docs/2.4/en/mod/core.html#documentroot

using php to move a file

I am trying to upload a client file to my server (from an html form using a "post" method), run a program on the $upldfile variable and then display the program results as downloadable links for the client.
My code is listed below and every time I run this I get the "file upload failed" notice.
Does anyone know if this is to with a permissions based problem or a server error or a code issue?
Thank you all in advance for any help offered
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . "uploads/" . basename( $_FILES["file"{["name"]);
$upldfile = move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
if ($upldfile){
echo "<p>File upload success.</p>";
} else {
echo "<p>File upload failed.";
}
ANSWER:
Changing the permissions appropriately,
and also
modifying the php.ini file to allow larger file uploads.
The code itself in the original file was correct.

Uploadify - files not showing up

I'm trying to use Uploadify (a jQuery plugin) with my CakePHP app. Locally (WampServer), it works great, but when I try it on my live server (Dreamhost), the files don't show up. I've properly chmod'ed the folders, checked the paths, etc, and I can't make any sense of why it isn't working. Here's upload.php:
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . "/app/webroot/posts/temp/";
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
$targetFile = str_replace(".", "_" . mt_rand(10000000,99999999) . ".", $targetFile);
move_uploaded_file($tempFile,$targetFile);
}
echo "1";
This script is definitely being run, but I've looked in the specified folder (and all over the filesystem), and the uploaded file(s) just aren't showing up! It's driving me crazy--hopefully someone has the answer to this. Please let me know if I should post any more code, and I will.
You could change $targetPath so it is relative to the document rather than the exact server path. I had a similar problem a while ago.
Turns out, it was this line:
$targetFile = str_replace(".", "_" . mt_rand(10000000,99999999) . ".", $targetFile);
This line was meant to append a random series of numbers to the filename to avoid collisions. However, as you can see, it's operating on the whole path, not just the filename. Well, my domain name is in my path (i.e. mydomain.com), and thus was getting changes to mydomain_12314123402.com, which obviously is a path that doesn't exist.
Man, I feel like an idiot!

Categories