Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Basically, I am trying to put an image file into a folder in my server and add the dir in MYSQL BD. It successfully puts the file into the folder and adds the path link to the row in the table.
Now, the problem: when I try to download the file and show it, it downloads but nothing shows, so I check the file in the FTP and it turns out the file size is 0.1kb. What am I doing wrong and how do I fix it?
I've done some research on the issue and I might be crazy but I think I'm the only one having this issue because I couldn't get help anywhere.
here's my code;
$ImageData = $_POST['image_data'];
$ImageName = date("D M d, G:i");
$ImagePath = "albums/$ImageName.jpg";
$UploadPath = __DIR__."/../".$ImagePath;
$img = $ImagePath; //to be inserted into DB row
//mysql ("INSERT INTO table...") query here
if($InsertSQL->rowCount()){
file_put_contents($UploadPath,base64_decode($ImageData));
// file_put_contents($UploadPath,$ImageData);
echo "Your Image Has Been Uploaded.";
}
N/B: I use base64_decode because I encoded it from android.
I expect the file to save to the server dir with the actual size.
Files are stored in $_FILES not $_POST.
Assuming that the field in your form is called image_data
$ImageTmpPath = file_get_contents($_FILES['image_data']['tmp_name']);
$ImageName = date("D M d, G:i");
$ImagePath = "albums/$ImageName.jpg";
$UploadPath = __DIR__."/../".$ImagePath;
$img = $ImagePath; //to be inserted into DB row
//mysql ("INSERT INTO table...") query here
if($InsertSQL->rowCount()){
file_put_contents($UploadPath,base64_decode($ImageTmpPath));
echo "Your Image Has Been Uploaded.";
}
So thanks to #RiggsFolly and #Rust, files are actually stored with $_FILES not $_POST
but what actually solved my issue was the
file_put_contents($UploadPath,base64_decode($ImageData));
it is supposed to be
move_uploaded_file(base64_decode($ImageTmpPath),$UploadPath);
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
After Uploading image it shows date in the name of image in uploadfolder.Instead
need to show ward number which is posted in $ward.The commentline was i changed
but its not working.please help me.
$image_name = "img_"."_".date("Y-m-d-H-m-s").".png";
// $image_name = "img_"."_".$ward.".png";
Try following code
$ward = $_POST['ward'];
$tmp_name = $_FILES["image"]["tmp_name"];//name of your input file name
$uploads_dir = "Your Upload idrectory path";
$image_name = "img__".$ward.".png";
move_uploaded_file($tmp_name, "$uploads_dir/$image_name");
The image name should have the ward name so that the saved file will have the same name. U need to get the ward name in some variable say $ward which is posted from the form, then add this $ward as an extension to the image name.
$ward = $_POST['ward'];
$uploads_dir = "";//path where the image needs to be stored in ur computer
$image_name = "img_".$ward.".png";
Try this code snippet in the original code and the extension of the image will be changed
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm creating a movie database and I have a Python script that downloads movie posters (and information) and stores them in a folder. For example, /var/www/html/images.
I also have a PHP file that displays a unique page for each movie, according to the ID. So for example there is a list page, the user clicks on a film and it goes to localhost/movielist.php?id=3 etc.
So far, the movie information is displaying perfectly. However, the images won't load.
In my python script, I save the movie poster image as the movie's name, so say if I had Finding Nemo's poster, the file name would be Finding Nemo.jpg. It saves it fine and I can look at the image in a folder.
The problem is that the image won't display on my PHP page.
This is the PHP code in my file:
<?php
$username="root"; $password="12345"; $database="videos";
$con = mysqli_init();
mysqli_options($con, MYSQLI_OPT_LOCAL_INFILE, true);
mysqli_real_connect($con, "localhost", $username, $password, $database);
mysqli_select_db($database);
if(isset($_GET["id"])){
$id = $_GET["id"];
} else {
require("listFull.php");
}
$res = mysqli_query($con, "SELECT id, title, yr, genre, plot, rating FROM films WHERE id = " . $id);
$movItems = array("Title" => "title", "Year" => "yr", "Genre" => "genre", "Plot" => "plot", "Rating" => "rating");
while($row = mysqli_fetch_array( $res )){
foreach($movItems as $k => $el){
echo "<h3>" . $k . "</h3>";
echo "<p>" . $row[$el] . "</p>";
}
echo "<img src='/var/www/html/images/".$row['title'].".jpg'>";
}
?>
This gets the row/movie's information according to the ID of the page and displays it. The image's name in the image folder is the same as the movie name in the database.
How can I get my images to display?
The issue is likely to be with your image path, this should be relative to the web root directory, not the actual path on the web server. So for example if your web root directory is /var/www/html and your images are stored in /var/www/html/images then your image path should be:
echo "<img src='/images/".$row['title'].".jpg'>";
Your problem is the img tag. First of all, the tag is malformed, the <at the beginning is missing.
Secondly, you shouldn't use /var/www/html/ as your image path because that directories are visible only in localhost. If you want to make them public you have to provide the correct url like www.mysite.com/images/Finding%20Nemo.jpg.
Finally, pay attention at the files' name: in http requests you can use spaces and other special characters (in the example above I substituted the space with %20) so make sure to substitute them.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a problem while trying to open a file and display its contents using php
My file called hello.txt
Here is my PHP code
<?php
$filename = 'hello.txt';
$filePath = 'c:\\Users\\bheng\\Desktop\\'.$filename;
if(file_exists($filePath)) {
echo "File Found.";
$handle = fopen($filePath, "rb");
$fileContents = fread($handle, filesize($filePath));
fclose($handle);
if(!empty($fileContents)) {
echo "<pre>".$fileContents."</pre>";
}
}
else {
echo "File Not Found.";
}
?>
I got this from
http://php.net/manual/en/function.fread.php
I keep getting error:
fread(): Length parameter must be greater than 0
Can someone help me please?
Although there are good answers here about using file_get_contents() instead, I'll try to explain wht this is not actually working, and how to make it work without changing the method.
filesize() function uses cache. You probably executed this code while still having the file empty.
Use the clearstatcache function each time the file change, or before testing its size :
clearstatcache();
$fileContents = fread($handle, filesize($filePath));
Also obviously make sure that your file is not empty ! Test it :
clearstatcache();
if(file_exists($filePath) && filesize($filePath)) {
// code
}
It needn't be that hard, and it certainly doesn't require you to read a file in binary mode:
if (file_exists($filePath))//call realpath on $filePath BTW
{
echo '<pre>', file_get_contents($filePath), '</pre>';
}
All in all, you really don't want to be doing this kind of stuff too much, though
If you need to read the entire file's content, there is a shortcut function
http://php.net/manual/en/function.file-get-contents.php
So you don't need to bother creating a file handler and closing it afterwards.
$fileContents = file_get_contents($filePath);
using file_get_contents method of php
echo file_get_contents("text.txt");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am using a script to back up files from ftp. code is below.
include "recurseZip.php";
//Source file or directory to be compressed.
$src='source/images/black.png';
//Destination folder where we create Zip file.
$dst='backup';
$z=new recurseZip();
echo $z->compress($src,$dst);
Now I want to get values to $src from source/files.txt which contains a list of file names.
My .txt file:
index.php.bk-2013-12-02
index.php.bk-2013-12-07
index.php.bk-2013-12-10
index.php.bk-2013-12-20
index.php.bk-2013-12-26
function.php.bk-2013-12-20
function.php.bk-2013-12-23
contact.php.bk-2013-12-23
contact.php.bk-2013-12-30
my source/files.txt contains 10 file names those need to be assigned as values to the variable $src I am using this script http://ramui.com/articles/php-zip-files-and-directory.html
how can I do that.?
any help will be very much appreciated.
Thanks.
Okay, you want to get the file name from each line of the .txt file.
<?php
$myFile = "files.txt";
$lines = file($myFile);
foreach($lines as $line){
$file = basename($line);
echo $file;
}
?>
Answer to your old question variant
You can use the basename() function. The manual says, "given a string containing the path to a file or directory, this function will return the trailing name component".
Now, you said "I want to get file name to $src from source/files.txt", so assuming from this, you are looking to get the file name i.e. black.png. This could be achieved using the basename() function as mentioned before.
<?php
$src='source/images/black.png';
$file = basename($src);
echo $file;
?>
Output
black.png
http://ideone.com/p2b4sr
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a website, where the user can write PHP/HTML code and save it to his computer. Everything is fine until the user types a slash (/).
The file saves into the client computer, but instead of saving the client code, it exports an PHP error (the file in the computer has a PHP code of an error). The file-saving code is the following:
$content = $_REQUEST['code']; //Get the code
$file = "file.php";
file_put_contents($file, $content); //Writes the content into a file
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename=$file');
readfile(dirname(dirname($con)) . '/'.$file);
The error only happens when the client uses slashes.
Any idea on why is this happening? )-:
EDIT:
This is one of those errors:
The code that i tried to export was the following:
/:
The thing that worries me, is that if I type exactly the same characters in different order (:/) then they export to my computer with no errors.
I see only one error - problem with readfile(\/105.2.php) in line 24 - so I tested it.
$file = 'file.php';
$con = '/:';
readfile(dirname(dirname($con)) . '/'.$file);
It gives me incorrect path \/file.php as in error message.
If I use $con=':/' it gives me correct path ./file.php
I only don't know what $con is. Maybe you have dirname(dirname($content)) in your oryginal code and $content = $_REQUEST['code']; => dirname(dirname($_REQUEST['code'])) => dirname(dirname("/:")) => "\"