I have a MySQL db with a table, that has the following field:
txtProductBowlImage: example " C:\BowlPhotos\Thumbs\MapleTi07-030tmb.jpg"
I want to use the data in that field to produce an image that I can print to a text file.
I read the data then try to echo it to the screen...and I see a small icon, but no image.
Here's the code:
<?php
$con = mysql_connect("localhost","xxxxxxx","zzzzzzzzzzzzzzz");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("scwdb", $con);
$result = mysql_query("SELECT * FROM tblsplintersbowlinventory WHERE txtVenue = 'Shoreline'");
while($row = mysql_fetch_array($result))
{
echo $row['intProductID'] . " " . $row['txtProductBowlCode'] . " " . $row['txtProductBowlImage'] . "]";
echo "<br />";
echo "<br />";
echo 'txtProductBowlImage: ' . $row['txtProductBowlImage'];
echo "<br />";
$img = $row['txtProductBowlImage'];
echo '...............$img: ' . $img;
echo "<br />";
echo "<img src=" . $img . ">";
echo "<img src='C://BowlPhotos//Thumbs//Ash07-013_btmb.jpg'>";
echo "<img src='Ash07-013_btmb.jpg'>";
echo "<br />";
echo "==================";
echo "<br />";
}
mysql_close($con);
?>
Note that I try 3 times to show an image:
1: using the url from the db and putting it in an echo statement
this url uses the '\' separater in the data.
2: imbedding a sample of the actual text path and filename in an echo statement
this url uses the '/' separater in the url.
3: imbedding only the filename, no path, in an echo statement
When I run this, I get the following output:
Echo #1 & #2 produce the icon, echo #3 shows a local copy of the image in the same folder as the php.
From this it appears that it is the path that is not being used, either with the '\' or '/' separater.
I assume the '\' may be seen as an escape, but why doesn't the url with the '/' work?
All below is wrong seeing as "Linking to local resources is disabled in all modern browsers due to security restrictions"
To link to a local image you use the format
<img src='file:///C:/path/to/image/pic.jpg'>
You can replace the backslashes with forward slashes using
$img = str_replace('\\', '/', $row['txtProductBowlImage']);
Now you can add the file:/// and the single quotes around the path
like so
echo "<img src='file:///" . $img . "'>";
With double quotes you could skip the dot operator to append strings
and use
echo "<img src='file:///$img'>";
The dollar $img will be recognized as a variable
It seams your trying to get a file from your local computer. So you have to run a webserver that hadle php
You might be trying to read an image where the server dont have acess.
try to place the file with your html or php file
OK, with JonOsterman's greatly appreciated aid, the final working code is:
<?php
$con = mysql_connect("localhost","root","Madge1938");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("scwdb", $con);
$result = mysql_query("SELECT * FROM tblsplintersbowlinventory WHERE txtVenue = 'Shoreline'");
while($row = mysql_fetch_array($result))
{
echo $row;
echo "<span>" . $row['intProductID'] . " " . $row['txtProductBowlCode'] . " " . $row['txtProductBowlImage'] . "</span>";
echo "<br />";
$img = $row['txtProductBowlImage'];
echo "<span> Original img: " . $img . " </span>";
$img = str_replace('\\', '/', $img);
echo "<br />";
$img = str_replace('C:/BowlPhotos/Thumbs/', 'Thumbs/', $img);
echo "<span>Folder img: " . $img . " </span>";
echo '<img src="' . $img . '">';
echo "<br />";
$img = str_replace('Thumbs/', '', $img);
echo "<span>Local img: " . $img . " </span>";
echo '<img src="' . $img . '">';
echo "<br />";
echo "<span>done</span><br />";
}
mysql_close($con);
?
There were a number of changes, but the biggest surprise was that the php could not open a folder not in the same folder or a sub folder of the php file! When I moved the entire folder to a sub of the php folder, the code above runs!
Also, as seen above, the HTML does not need a "file:///" for a local file...
And I should mention, this is intended to run only on my local server - not on the web. It is just for my own use to keep track of the wood bowls I make and sell.
Again, many thanks to Jon...
Related
I want to synchronize files between two servers.
The idea is to check if a file exists on the "local" server and if true, check if it the same as a remote server. If not, the update it.
If it doesn't exist, add it.
The code seems to work but always one file is changed, even when it hasn't and its a different one each time.
Is there an error or what is the reason?
if (file_exists($img)) {
$image_hashfile_remote = sha1_file($image_url[$urlIndex]);
$image_hashfile_local = sha1_file($img);
if ($image_hashfile_local == $image_hashfile_remote) {
$num_same++;
} else {
file_put_contents($img, file_get_contents($image_url[$urlIndex]));
echo "Image file as changed since last synchronization. " .$img . " updated. <br />";
echo $image_hashfile_local . " " .$image_hashfile_remote . "<br />";
$num_saved++;
}
} else {
file_put_contents($img, file_get_contents($image_url[$urlIndex]));
echo "New image file found. " . $img . " added. <br />";
$num_saved++;
}
I have a script in php that is used to upload files to a server. It was working first but i dont know why its not working again. It shows no error but the file is not still uploaded to the directory that i assigned to hold all uploaded files. Here is the part that takes care of the upload:
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Uploaded: " . basename($_FILES["file"]["name"]) . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
//by default the size of the file is in bytes so u need to divide by 1024 in order to bring it to KB
echo "Temporarily stored in: " . $_FILES["file"]["tmp_name"] . "<br />";
$target="/file/" . basename($_FILES["file"]["name"]) . "<br />";
}
if(move_uploaded_file($_FILES["file"]["tmp_name"], $target))
{
echo "<h2>" . "The file has been stored on the server" . "<br />" . "<h2 />";
echo "New storage location is : " . '<a href="/public/files/" >' . $target . '</a>';
?>
<html>
<body>
<div align="right"><a href="/public/files/" >Uploaded files</a></div>
<br />
</body>
</html>
<?php
}
else
{
echo "<h2>" . "Error while saving the file to the server." . "<br />" . "File wont be found in the uploaded files directory" . "<br />" . "<h2 />";
echo "The error says: " . $_FILE["file"]["error"] . " What do we do now?" ;
echo"<pre>".print_r($_FILES,true)."</pre>";
?>
<?
/file/ is a directory in the root of your server's filesystem, which almost certainly doesn't exist. move_uploaded_file() works at the filesystem level and has absolutely NO awareness of your site's URI structure. You probably want something more like:
move_uploaded_file(...,. $_SERVER['DOCUMENT_ROOT'] . '/file/');
^^^^^^^^^^^^^^^^^^^^^^^^^^^---- add this
so that the file gets moved to a /file subdir of your site's root directory.
What I want to do is,
I am initializing $message variable for mail body.
And I has to iterate through loop if multiple files are there.
So how can i iterate loop and store values in a variable.
Like,
$message = "Dear " . $fname . " <br/>
<b>Manuscript and Other Documents :</b> <br/>
Source File : " .$file1 . " <br/>
Source PDF File : ".$file2 . " <br/>
Cover Letter : " . $file3 . "<br/>
Supplementary Files : " . while($row=mysql_fetch_array($supplementary)){ echo $row[0] } . ";
So how can i do this if i have multiple supplementary files?
To do this, simply append the data to the message variable within the while loop using the string concatenation operator.
For example:
$message = "Dear " . $fname . " <br/><b>Manuscript and Other Documents :</b><br/>
Source File : " .$file1 . "<br/>Source PDF File : ".$file2 . " <br/>
Cover Letter : " . $file3 . "<br/>Supplementary Files : ";
while($row = mysql_fetch_array($supplementary)) {
$message .= echo $row[0] . '<br />';
}
First, do this:
$suplArray = array();
while($row=mysql_fetch_array($supplementary)){
$suplArray[] = $row[0];
}
Now, you have the strings in an array. I don't know what format you want, but you can do something like
$suplStr = implode(',', $suplArray);
Then in your string, you can have:
... "Supplementary Files : " . $suplStr;
<?php
$file= $_FILES["file"]["name"];//file selected in form
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
move_uploaded_file($_FILES["file"]["tmp_name"],
"c:/EasyPHP-12.1/www/new website/upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "c:/EasyPHP-12.1/www/new website/upload/" . $_FILES["file"]["name"];
if(($_FILES['file']['size'] >0))
{echo 'imagetrue';$con=mysql_connect('localhost','abc','YES') or die ("con");;
$db=mysql_select_db("website",$con) or die ("db");
$query=mysql_query("insert into website.picture (imagew) values ('$file')") or die('query'); //storing in db}
//echo $_FILES['file']['error'] ;
$query2= mysql_query("select * from website.picture");
$res=mysql_fetch_array($query2);
foreach($res as $row){
$str = "c:/EasyPHP-12.1/www/new website/upload/ ".$row['imagew'];
echo '<img src="'.$str.'" alt="no">' ;}
?>
i am using this script to store image and then displaying it on webpage but it is not working it only displays empty thumbnails...
I'd be very suspect about using absolute paths like c:/EasyPHP-12.1/www/new website/upload/
I'f you're then accessing the page from http://127.0.0.1/new-website (which I presume you are - or something similar) then having windows system pathnames could be a problem - do browsers even read the local filesystem like that ?
Also, it makes it very un-portable for when you move it to another server.
I'd suggest $str = 'upload/'.$row['imagew']; So it's relative to the document hosted in (I presume) 127.0.0.1/new website/image-viewer.php (or whatever you called it)
Check the return value of move_uploaded_file() - if it's false then it has failed. If that's failing try using the relative path :
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
This again presumes your script is sitting in 127.0.0.1/new-website
EDIT - NOTE ::: Is your form using enctype="multipart/form-data" ? If it's not then the images never get to the server !
Hi I am trying to get images to load into a page using the file names from an array,
This is what I have so far
<?php
$i=0;
$img=array("1.png","2.png","3.png","4.png");
while ($i<count($img))
{
echo "<img class='loadin' alt='imgg' src=" . "'http://www/images/" . $img[i] . "'" . "/" . ">" . "<br/>";
$i++;
}
?>
It seems to ignore the file name and just enters:
http://www/images/
as the source and ignores the file name from the array
Any Help would be great Thanks
Mikey
You forgot the dollar sign with your $i variable: $img[$i]
EDIT:
(btw. using a foreach-loop would be easier...)
foreach($img AS $filename) {
echo "<img class='loadin' alt='imgg' src='http://www/images/" . $filename . "'/><br/>";
}