I am tryin to upload file in mysql database using php with codeigniter framework.
Following my code. Its just saving first name of file in database but actual file in not storing at the given path.
My code is -
move_uploaded_file($_FILES["userfile"]["tmp_name"] , "uploads/diagnosis_report/".$_FILES["userfile"]["name"]);
$data['file_name'] = $_POST["userfile"]["name"];
I believe:
$data['file_name'] = $_POST["userfile"]["name"];
Should be:
$data['file_name'] = $_FILES["userfile"]["name"];
// ---------------------^
$upload_path$_POST["userfile"]["name"] is just the file name. If you want to save it with the path you need to do something like this:
// For relative path
$data['file_name'] = "uploads/diagnosis_report/".$_FILES["userfile"]["name"];
// For absolute path
$data['file_name'] = dirname(__FILE__)."/uploads/diagnosis_report/".$_FILES["userfile"]["name"];
Honestly what I would do is set an upload path variable first like so:
$upload_path = dirname(__FILE__)."/uploads/diagnosis_report/";
Then you can use it over and over again like so:
move_uploaded_file($_FILES["userfile"]["tmp_name"] , $upload_path.$_FILES["userfile"]["name"]);
$data['file_name'] = $upload_path.$_FILES["userfile"]["name"];
Hope this helps.
Related
I have a file path on a table, what I am trying to do is create the image from the file path and then save the 'new' file with a different name.
Because I have only the file path, I do not now how to create the image object so that I can then getClientOriginalExtension(); and save that to the database. I have tried the following:
$img = $var->image_path;
$file = file_get_contents($img);
$filename = time() . '.' . $file->getExtension();
Image::make($file)->resize(300, 300)->save( public_path('/test' . $filename ) );
However the script errors: Call to a member function getExtension() on string what would be the right way with the file path, create the object, change the name of the file, ensure the right extension is set (maybe outside scope of this q) and then save the newly created image to a different folder and save the newly created image path to the database.
I hope that makes sense.
Update: should I use file_put_contents() instead?
Achieved what I wanted using the copy() and iterating over each image and adjusting the image relatively.
$title = $var->name;
$string = str_replace(' ', '-', $title); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
copy($img, public_path('/test' . $string . time()));
http://php.net/manual/en/function.copy.php
I used php real path to get actual path of files and directory to delete and after deleted i will print all deleted
items. But my problem is that it also show the real path where the file source is and i don't want to show it to users
is there any way i can hide the pay and only show the file example.
I don't like it to look like this
[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg
[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/room_home_1.jpg
[Directory]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder
Is there anyway i can make it look this way using rejex or any method please i need help i have to remove /mnt/wef66/d2/81/557642661/htdocs/
[File]: www.example.com/useruploads/myfiles/imagefolder/mosaic_1.jpg
[File]: www.example.com/useruploads/myfiles/imagefolder/room_home_1.jpg
[Directory]: www.example.com/useruploads/myfiles/imagefolder
Maybe using something like this
echo preg_replace("/mnt/wef66/d2/81/557642661/htdocs", "www.example.com", "[File]: /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg");
$path = realpath($parentBas);
I would use configuration variables $privatePath and $publicPath.
So you can concat whichever you want to the relative paths to your directories or files.
For your example:
$privatePath = '/mnt/wef66/d2/81/557642661/htdocs/';
$publicPath = 'www.example.com/';
$pic1RelativePath = 'useruploads/myfiles/imagefolder/mosaic_1.jpg';
$pic1privatePath = $privatePath . $pic1RelativePath;
// /mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg
$pic1publicPath = $publicPath . $pic1RelativePath;
// www.example.com/useruploads/myfiles/imagefolder/mosaic_1.jpg
I think this is easier and more efficient than replacing the paths with regex.
EDIT:
If you have all the real paths in an array, you can loop through it and replace easily all the private paths with the public paths this way:
$paths = [
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/mosaic_1.jpg',
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder/room_home_1.jpg',
'/mnt/wef66/d2/81/557642661/htdocs/useruploads/myfiles/imagefolder'
];
foreach ($paths as &$path) {
$path = str_replace($privatePath, $publicPath, $path);
}
print_r($paths);
I have a data uri variable in php
$imageURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAYAAACadoJwAAAgAElEQ…ACIiACIiAC5U1AAqS891erEwEREAEREAEREAEREIFAEfj/bfXX..."
I am trying to insert this into a pdf using fpdf for which I need to convert this into a image I guess. I tried doing something like
base64_decode($imageURL);
but this does not work. How I successfully insert this data uri into pdf.
$image_content = base64_decode(str_replace("data:image/png;base64,","",$imageURL)); // remove "data:image/png;base64,"
$tempfile = tmpfile(); // create temporary file
fwrite($tempfile, $image_content); // fill data to temporary file
$metaDatas = stream_get_meta_data($tempfile);
$tmpFilename = $metaDatas['uri'];
Now you can use that image into fpdf like:
$pdf->Image($tmpFilename,null,null,0,0);
Or you can specify image type by adding image type parameter like this:
$pdf->Image($tmpFilename,null,null,0,0,'PNG');
Please check to http://www.fpdf.org/en/doc/image.htm
I need to change the folder that "relative include paths" are based on.
I might currently be "in" this folder:
C:\ABC\XYZ\123\ZZZ
And in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\ABC\XYZ\Source
And realpath('.') would = 'C:\ABC\XYZ\123\ZZZ';
If however, realpath('.') were "C:\Some\Other\Folder"
Then in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\Some\Source
How do I change what folder is represented by '.' in realpath()?
Like this:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
// Some PHP code here...
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
How can I change the folder represented by '.', as seen by realpath()?
The function chdir() does this.
For example:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
chdir('C:/Some/Other/Folder');
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
Use the chdir() function.
Change your current working directory with chdir()
http://us.php.net/chdir
I am using getimagesize() to get teh size of my image. I have image in my sub-directory when i am passing file name with sub-directory i ma facing an error:
There is no such file or directory
here is my Code:
<?PHP
function resize($img,$max_w){
$max_h=$max_w;
list($img_w,$img_h) = getimagesize($img);
$f = min($max_w/$img_w, $max_h/$img_h, 1);
$w = round($f * $img_w);
$h = round($f * $img_h);
return array($w,$h);
}
$filename="user_img/"."1256115556.jpg";
$resize=resize($filename,667);
$w=$resize[0];
$h=$resize[1];
?>
instead of this when i passing
$filename="1256115556.jpg";
file from my main directory the function is running perfectly. So please help me, How to pass file with sub-directory.
Pass your directory name inside getimagesize(). Because the file name is a String.
list($img_w,$img_h) = getimagesize("user_img/".$img);
Its working fine now.
If you're on a windows box then you might try a "\" and not a "/" in your path.