Execute PHP file located in another folder - php

I have a script that uploads a file. It then creates a new folder and moves the uploaded file to that folder along with the another script.
After the file is moved, I need to run a second script that resides in the new folder. But I need the second script to use its path and NOT the path of where it is called/executed from. (the upload script)
This is how I have tried to execute the script from the upload script.
exec("php-cli uploads/".$dirname."/".$file_name."/generate_thumbs.php");
I have error reporting on but nothing is generated. Any ideas?

If you want to change working directory, use chdir() before exec().
You can pass either absolute or relative directory name.
To execute PHP from PHP you can use include instead of exec().
edit your upload script to:
<?php
// ... upload the file ...
// ,,open'' the target directory
chdir("uploads/$dirname/$file_name/");
// start the thumbs script
// this filename is relative to "uploads/$dirname/$file_name/"
include "generate_thumbs.php";
?>

What you should do is within your second script to use path to where the script is.
As found in the answer:
Get absolute path of current script
From there you can write your script to operate from where it is as long as you can resolve where it is.

Related

I can't find the file images after uploading them with php function move_uploaded_file

Could someone please help me understand how to find the location of the file and directory when using this PHP function:
move_uploaded_file($_FILES['images']['tmp_name'], "tmp/".$file_name);
I'm trying to upload images (jpg, jpeg, png, gif), but I can't find the file directory of the images after uploading.
Example output after uploading file is: /tmp/phpqZRudj
I can't locate this file.
The path of the destination file is relative to the "current working directory", i.e. the directory that the script runs from. The function will return FALSE if the file cannot be moved for some reason.
I don't think "tmp/".$file_name is going to work because of the pathing. If, for example, your script is running in a directory called public, you would set the destination to "/tmp".$file_name to put it in a directory under public named tmp. (And make sure the directory exists). Notice the leading /. ("slash" directory notation works on both windows and *nix). You would then find the moved file in public/tmp.
To display errors you need to set the "display_errors" configuration option On.
ini_set("display_errors",1); will accomplish that.

PHP File Upload Directory

I am working on a file upload script. I know that every part of the script is valid, except for the path I am trying to specify.
I am not exactly sure what the path should be relative to. Does it need to be relative to the temporary folder that PHP stores the file in, or relative to the location that my script is running from?
Also, I'm not sure how specific I need to be. The directory where I am trying to upload the file is: www/mm/uploads
It would be relative from the location that the script is running from.

PHP Upload file to Apache server fails

I have a form that submits a picture, screenshot is the name of the input tag.
In the php script I can print out $_FILES['screenshot']['name'], but when I try to get the temp folder it is saved in on the server with $_FILES['screenshot']['tmp_name'], I get nothing.
Anyone know why? Seems like the file gets to the temp folder, but I can't get the path.
They're stored in /tmp and removed on script completion as far as I remember..
You need to use move_uploaded_file to move the file to an accessible folder..
I.E.
move_uploaded_file($_FILES['screenshot']['tmp_name'], '/uploads/' . $_FILES['screenshot']['name']);

php uploading file?

I just want to know that if I am using move_uploaded_file function and use two argument first as the name of file and second as the destination.
Normally I have uploaded many files with class uploader but now I want to give the destination as http://www.example.com/testing/
Although I have given 777 permission to this folder but when I try to execute the upload code error came
Destination directory can't be created. Can't carry on a process.
How can I upload the file local to server using php code?
If you are passing http://www.mydomain.com/testing/ as the target, this is wrong.
You can't just upload files to servers via HTTP, you only can do that to local folders, can you paste the exact code so we can know better what are you trying to do?
move_uploaded_file is a server-side function, so all the paths should be specified server side.
If your upload.php (i'm assuming the filename) is in the main directory of the website www.mydomain.com/ which is probably /home/youruser/public_html/ then you can specify the destination as simply "testing/"
If your upload file is in some nested directory, then it may work better to specify the full destination path:
/home/youruser/public_html/testing
good luck

move_uploaded_file not working

I'm uploading files via JS and storing the temp path in the session.
Than i use the following code to move the files.
if(move_uploaded_file($_SESSION['temp_img'][$key]['path'], $dest.$bigimg)){
$dest and $bigimg are defined earlier in the script with the id from the database.
Any Ideas or alternatives ?
MANCHUCK's answer was close but not quite there. You must call move_uploaded_file within the script where the file was uploaded. You cannot do what you're doing, that is, "storing temp path in the session" because that path is only valid for one request.
From the PHP manual:
The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.
(Emphasis mine)
move_uploaded_file checks that a file has been uploaded to that page. You are actually uploading the file to a different PHP script then storing in a session. Instead of using move_upload_file use rename.
What is the output of $_SESSION['temp_img'][$key]['path'], also do you have permission to write to the web directory your placing the files. You may need to set it to 777 for some hosts to allow the webserver to write there.

Categories