When I try and upload a file as follows:
$name = $img->getClientOriginalName();
$fullPath = Product::getUploadPath(); // Returns public_path(); with custom directory appended
$uploaded = $img->move($fullPath, $name);
I get the below error:
Could not move the file
"/private/var/folders/1l/fxl7spqj2p113fpffm3k0sy00000gn/T/phpIgpWGy" to
"/dir/subdir/subsubdir/subsubdir/public/backend/images/products/pic.jpg" ()
For interestsake, I built the entire resolve path for the image, and passed that as an argument to the move() method. Memory served that it has worked previously.
$uploaded = $img->move($fullPath.'/'.$name);
No exceptions are thrown, however, the uploaded file, now becomes a directory:
"/dir/subdir/subsubdir/subsubdir/public/backend/images/products/pic.jpg/"
I'm very close to driving my fist through my screen.
Could be a permissions issue - the user running the PHP process will need to have write access to the directory where the file will be saved. If you can stomach chmod'ing the directory to 777 for testing, that would at least provide an immediate yes or no to the permissions possibility.
Also, be sure that the directory that file is to be moved to exists - Symfony's UploadedFile::move() method falls back to PHP's move_uploaded_file() function, which according to a comment on the documentation will not create missing directories when moving a file.
When it comes to uploading files, we've all been there before. Can be maddening.
Related
I am trying to move a file from a temporary path to the correct path. However no matter what I tried it always sends back this file not found error:
League\Flysystem\FileNotFoundException
File not found at path: var/www/html/storage/app/public/covers/tmp/608c07a082451-1619789728/81-536x354.jpeg
I am using sail thus there are 'var/www/html/' in the path, even though there is no problem with this path in the container since it is possible to reach this path from the frontend fetch API and that is how the temp file was saved.
Here is the code in the controller:
//...
$temp_file = TempFile::where('folder', $cover_folder)->first();
if ($temp_file) {
// retrieve file path and target path
$filepath = storage_path("app/public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}");
$target_path = storage_path("app/public/covers/{$course->id}/");
Storage::move($filepath, $target_path);
$course->cover_img = "{$target_path}/{$temp_file->filename}";
$course->save();
// remove temp directory and temp file record
rmdir(storage_path("app/public/covers/tmp/{$temp_file->folder}/"));
$temp_file->delete();
}
The error traceback highlighted on this line:
Storage::move($filepath, $target_path);
Also tried to use Storage::disk('public')->move(), or public_storage, or Storage::path(), or only pure string, all did not work.
Symlink is set from public/storage to storage by running php artisan storage:link.
I have searched as much as I could find on laracast, stack overflow, or github, but could not reach a solution. Does anyone have any idea on this issue?
Really really appreciate it.
dont use storage_path function because method Storage::move( have prefix path [app_path]/storage/app/
Storage::move("public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}", "public/covers/{$course->id}/");
i have a constant variable named SADMIN
define('SADMIN','http://localhost/synthesis_study_material/student_admin/');
and i am trying to use SADMIN constant in my move_uploaded_file function
move_uploaded_file($_FILES['name']['tmp_name'], SADMIN."include/uploaded/epub/".$_POST['name']);
my current file is in
http://localhost/synthesis_study_material/synthesis_notes_admin/index.php
file is not getting uploaded in expected folder and move_uploaded_file is not giving any warning or error
The destination of move_uploaded_file needs to be a file path, not a url. To clarify this your webserver has a specified folder on the file system set as the web root. This might be something like '/var/www'. This means when you go to 'http://localhost/' it will look for a file such as '/var/www/index.php' or '/var/www/index.php'.
the destination might be an absolute path or relative, so you might supply 'synthesis_study_material/student_admin/' and that might be the same as '/var/www/synthesis_study_material/student_admin/' (depending on your web root and where move_uploaded_file is called from). Note you can use dirname(__FILE__) to get the path of the current php file. Also note that move_uploaded_file will not create directories for you if they don't exist.
I suggest starting with a simple example such as move_uploaded_file($_FILES['name']['tmp_name'], 'test-upload.txt'); to see if anything gets uploaded, and make modifications from there.
I am trying to create a temporary directory but I am getting following error
Warning: mkdir(): File exists
However when i checked the directory actually doesn't exist.A typical value for $tmp is /tmp/testKanfEt
$tmp = tempnam(sys_get_temp_dir() , 'test');
echo $tmp;
mkdir($tmp);
What am i missing here ?
The function tempnam() creates a file in given path and returns the full path if created successfully. You are attempting to make a directory with the same path as the file.
But in this case, tempnam() is being used wrongly in my opinion.
It should be used when trying to make tmp files in any other directory than the o.s. tmp folder.
Why? Because making files in the tmp directory you should not care about the file name because once the lock has been released on a file (with fclose() or end of script execution for example) you cannot guarantee the file still being there.
Instead, use tmpfile() as it returns a file handle directly creating a file in the tmp directory.
And if you really need the file name, you still could use tempnam() or retreive it from the handle like so:
echo stream_get_meta_data(($fh=tempfile()))["uri"];
Hopefully easy question, I have a desktop application that allows the user to upload a file to a server using a form, the form sends the data to a protected file on the site like this. Site_root/protected_folder/myfile.php . If you use php file upload commands normally you'd be operating in the 'protected_folder' directory, which I don't want.
I want to add stuff to the images file on the root directory like this Site_root/images/ , how would you go about doing this without going the ftp root?
The usual method is to call move_uploaded_file(), where you set the destination path to your liking. The file name in $_FILES['tmp_name'] normally points to a temporary folder and it's subject to be removed without prior notice.
You can either use an absolute path like /path/to/images/ or use a relative path like ../images/
Assuming you're using move_uploaded_file the second paramater takes the directory that you wish to upload to. Perhaps showing you code may help if this post doesn't.
move_uploaded_file() will allow you to place uploads relative to the root directory if you simply start your path with a slash like
$newFileDir = '/username/public_html/websiteroot/subdir/yourfile.jpg';
move_uploaded_file($_FILES['postname']['tmp_name'],$newfileDir);
you can simply use copy() and double dot (../) in path to specify root directory to copy the uploaded file. I'm using the same. You may want to change the file name so that there will be unique filename in the directory error will occur. extension will also be same.
//
$filename = stripslashes($_FILES['postname']['name']);
$extension = getExtension($filename);
$newfilename ='photo_your_filename'.time().'.'.$extension;
$newFileDir = '../subdir/'.$newfilename;
copy($_FILES['postname']['tmp_name'],$newfileDir);
I am currently working on a Joomla! website. I am using the Jumi extension (which allows custom scripts to be made and executed within Joomla! itself) to create a simple file upload tool. The problem is that I get the following error:
Warning: copy(C:/xampp/htdocs/images/1253889508.jpg) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\Joomla\components\com_jumi\jumi.php(25) : eval()'d code on line 61
The offending code is as follows:
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname='C:/xampp/htdocs/images/'.$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
The full code is available here. Once I get this working, I will be modifying it to meet the needs of my website.
I'm not sure if the problem is a permissions issue with Jumi or if there is some other problem. The best I can tell is that for whatever reason, the temp file is not being created.
Thanks for any tips you may have!
Try this:
if(move_uploaded_file($_FILES['image']['tmp_name'], $newname)){
// move worked, carry on
}
And use relative paths instead of absolute ones.
does your C:/xampp/htdocs/images directory actually exists?
if not create it manually or with mkdir()
also try to use the constant DIRECTORY_SEPARATOR instead of hardcoding slashes
Also you should use the move_uploaded_file() for this and not the copy() function.
And never hardcode absolute paths into your scripts! instead get the root path and preferibly set it as a constant, this is mostly done with the dirname() function in the entry file, but joomla allready has a constant you can use for this.
Forward slashes in the $namename path rather than backslashes?