this might be a duplicate but dont know the reason why i got this error.. this code is fine when im using another laptop which the php version is 5.6 but when im using a laptop with php version 5.4 i got an error..
here is the code that im using..
public function uploadImg($file, $newname){
$path = "../valenciamd/captured_images/";
$fileparts = pathinfo($file["name"]);
$name = $newname . $fileparts["extension"];
if( is_dir($path) === false ){
mkdir($path);
}
$i = 0;
$parts = pathinfo($name);
// while (file_exists($path . $name)) {
// $i++;
// $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
// }
$name = $parts["filename"]. "." . $parts["extension"];
$success = move_uploaded_file($file["tmp_name"],
$path . $name);
chmod($path . $name, 0777);
return $path . $name;
}
$img = $reg->uploadImg( $_FILES['image'], $patientID.'.');
Are you sure that your php version is 5.4, maybe it's below 5.2 $path_parts['filename'] is only added since php 5.2 so you will get nothing. This is not an error, just update your php version.
Or if you do not want to update your php version, you can use
$parts['basename'] instead of $parts["filename"]. "." . $parts["extension"]
try to use the recursive directory creation
read the mkdir manual http://php.net/manual/en/function.mkdir.php
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive =
false [, resource $context ]]] )
Related
Below I have left my code. It currently works in my development environment (localhost), but when I push the changes to my live server it seems like my php doesn't create the folder/file.
public static function saveImage($image, $name, $path = '')
{
$img_data = explode(',', $image);
$mime = explode(';', $img_data[0]);
$data = $img_data[1];
$extension = explode('/', $mime[0])[1];
if(!file_exists('../public/media/img/' . $path)){
mkdir('../public/media/img/' . $path, 0755);
echo('Test1');
}
echo('test2');
file_put_contents('../public/media/img/' . $path . $name . '.' . $extension, base64_decode($data));
return 'media/img/' . $path . $name . '.' . $extension;
}
Locally it will hit echo('test1') the first time, then it will only hit echo('test2'). When its on the server it always hits the echo('test1')
By default mkdir is not create a path recursively. An example if on your server you dont have a ../public/media folder, mkdir returns false and dont create a path.
To solve this pass a third parameter to mkdir as true:
mkdir('../public/media/img/' . $path, 0755, true);
Do yourself a favour and use absolute pathes...
You can use the constant __DIR__ to evaluate the folder in which the script actually resides.
Relative pathes are calculated from the current working directory, which can be different than __DIR__
i have this code:
$a ="/Assets/ProductImages/oa/91/2239754/6/5151010073180_1_org_zoom.jpg";
$b ="/home/cfnic/domains/modmania.ir/public_html/image/Assets/ProductImages/oa/91/2239754/6/5151010073180_1_org_zoom.jpg";
$path = '';
$directories = explode('/', dirname($a));
foreach ($directories as $directory) {
$path = $path . '/' . $directory;
if (!is_dir('/home/cfnic/domains/modmania.ir/public_html/image/' . $path)) {
mkdir('/home/cfnic/domains/modmania.ir/public_html/image/' . $path, 0777,true);
}
}
it only create directory (Assets) and (ProductImages)
what am i doing wrong?????
If your purpose is to create directories recursively you can use mkdir once .
This is the prototype of the function taken from the PHP manual
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
so the only thing you really need is:
mkdir('/home/cfnic/domains/modmania.ir/public_html/image'.dirname($a), 0777,true);
without the foreach loop.
I'm saving files locally in Laravel, however I'm having issues getting the right URL and accessing the files.
I've setup a symlink with Artisan:
php artisan storage:link
When saving, I add public/ to the name, so the files are placed in the /storage/app/public/ directory, which works.
if ($request->hasFile('files')) {
$files = array();
foreach ($request->file('files') as $file) {
if ($file->isValid()) {
$name = time() . str_random(5) . '.' . $file->getClientOriginalExtension();
Storage::disk('public')->put($name, $file);
$files[] = $name;
}
}
if (count($files) > 0) {
$response->assets = json_encode($files);
}
}
The name is stored in the database
["1524042807kdvws.pdf"]
Then the assets are returned as part of a JSON object via my API for Vue
if (count($response->assets) > 0) {
$assets = array();
foreach (json_decode($response->assets, true) as $asset) {
$assets[] = asset($asset);
}
$responses[$key]->assets = $assets;
}
Which returns http://127.0.0.1:8000/1524042807kdvws.pdf but that 404s. I've gotten myself a little confused I think, so any pointers or help would be appreciated.
So I found my answer on another post
I needed to wrap the $file in file_get_contents();
Storage::disk('public')->put($name, $file);
and instead of asset() I used:
Storage::disk('public')->url($asset['file']);
Check this docs. https://laravel.com/docs/5.5/filesystem#the-public-disk
As it shows there instead of
$name = 'public/' . time() . str_random(5) . '.' . $file->getClientOriginalExtension();
you can just have
$name = 'time() . str_random(5) . '.' . $file->getClientOriginalExtension();
Storage::disk("public")->put($name, $file); // assuming you have the public disk that comes by default
Then when you want to get an url, you can use the asset function
asset('storage/foo.txt');
I'm using the mailReader script found here: https://github.com/stuporglue/mailreader
I am trying to copy the file(s) after upload to another folder.
The file(s) upload correctly to the folder where the script resides.
When I try to run a copy command, the filename variable is empty.
Here is the portion of the code I am working with: The last three lines are what I added.
private function saveFile($filename,$contents,$mimeType = 'unknown'){
$filename = preg_replace('/[^a-zA-Z0-9_-]/','_',$filename);
$unlocked_and_unique = FALSE;
while(!$unlocked_and_unique){
// Find unique
$name = time() . "_" . $filename;
$name = substr_replace($name,".pdf",-4); // added 1-19-2016
while(file_exists($this->save_directory . $name)) {
$name = time() . "_" . $filename;
$name = substr_replace($name,".pdf",-4);
}
// Attempt to lock
$outfile = fopen($this->save_directory.$name,'w');
if(flock($outfile,LOCK_EX)){
$unlocked_and_unique = TRUE;
}else{
flock($outfile,LOCK_UN);
fclose($outfile);
}
}
fwrite($outfile,$contents);
fclose($outfile);
if (copy($this->save_directory.$name, "/attachments/" . TRANS_ID . "/". $name)) {
unlink( $this->save_directory.$name );
}
I receive confirmation by email that the file(s) are uploaded, then another email with the error message.
Warning: copy(/attachments/W7652222-546/1453406138_residential-print_from_td.pdf): failed to open stream: No such file or directory in /home/myhost/public_html/mailreader/mailReader.php on line 224
224 being the line number of my added code.
The source filename is missing from in front of /attachments...
Anyone have any thoughts?
$name is defined in the while loop and may not be accessible on the upper scopes my suggestion is to change your code to this:
private function saveFile($filename,$contents,$mimeType = 'unknown'){
$filename = preg_replace('/[^a-zA-Z0-9_-]/','_',$filename);
$unlocked_and_unique = FALSE;
$name = '';
while(!$unlocked_and_unique){
// Find unique
$name = time() . "_" . $filename;
$name = substr_replace($name,".pdf",-4); // added 1-19-2016
while(file_exists($this->save_directory . $name)) {
$name = time() . "_" . $filename;
$name = substr_replace($name,".pdf",-4);
}
// Attempt to lock
$outfile = fopen($this->save_directory.$name,'w');
if(flock($outfile,LOCK_EX)){
$unlocked_and_unique = TRUE;
}else{
flock($outfile,LOCK_UN);
fclose($outfile);
}
}
fwrite($outfile,$contents);
fclose($outfile);
if (copy($this->save_directory.$name, "/attachments/" . TRANS_ID . "/". $name)) {
unlink( $this->save_directory.$name );
}
I hope this solves your problem
I ended up defining a constant of email_id in the private function saveToDb, then running a script after everything else is finished to query the table using the email_id and looping through the records moving the files.
I have following error:
Error type - 2: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 25: in /var/www/hosting/com/galerielaboratorio/scripts/functions.php on line 766
Here is the function:
function getThumbName($photo, $name = 'thumb') {
$ext = preg_replace ("/.*\./", "", $photo);
$photo = preg_replace ("/\.". $ext ."$/", "" , $photo). "." . $name . "." .$ext; // this line causes the error
return $photo;
}
Thanks for help in advance.
Cannot read your expression, but you missed one ). ()-pairs define expression groups. One ( without its corresponding ) is just invalid (and thats what the error message tries to tell you). If you want to have a literal ( you must escape it \(
However, have a look at pathinfo(), explode() and str_replace(). This is not a scenario for regular expressions
$ext = pathinfo($photo, PATHINFO_EXTENSION);
$photo = basename($photo) . '.' . $name . '.' . $ext;
Not sure where it goes wrong; it's not clear from the code, as you're building a dynamic regular expression. Anyway, here's an alternative for you to enjoy, which contains slightly more error checking than your original function.
<?php
function getThumbName($photo, $name = 'thumb') {
if( $ext = getExtension( $photo ) ) {
return basename( $photo, $ext ) . 'thumb.' . $ext;
}
return false;
}
/**
* Returns the file extension if it exists, false otherwise.
* #param string $filename
* #return string|bool
*/
function getExtension( $filename ) {
return pathinfo( $filename, PATHINFO_EXTENSION );
}
echo getThumbName( 'foo.jpg' ) . PHP_EOL; // foo.thumb.jpg
echo getThumbName( 'slightly.more.complex.gif' ) . PHP_EOL; // slightly.more.complex.thumb.gif
echo getThumbname( 'This is where it gets interesting' ) . PHP_EOL; // false.