Jump to content
Larry Ullman's Book Forums

Calling Uploaded Files Into For Loops In Tables


Recommended Posts

hey its me again guys I'm getting frustrated, I need to post a thumbnail into a table using the while loop and $row now the query calls from one table that stores the files id number from the uploads table how would I get it to display the file which is an image type in a thumbnail. pretty much need too know how to define the image from the uploads for the table while getting the image id from another table. and then to display it using the while loop so far this is what I have.

 

image1 is in another table stored as an ID number only

 

<td align="left">' . $row['image1'] . '</td> it will display the images id number I need to get the file and type from the uploads table then thumbnail it.

 

sorry for the vagueness but its 4:20 am and I'm past tired. so I would need to do something like this

 

I would make a query that calls the information from the uploads table first then run this?

 

$image1 = $row['image1'].$row['type']; ??? would that define $image1 as id.type?

 

then how would I thumbnail it in the table row? i know html is not part of this forums topics but I just want to get past this prick in my neck...

 

if you need any other info just ask.

Link to comment
Share on other sites

Actually asked this wrong because every row will call a different file ID I need to put an image element in the tablerow where image1 is the $row['image1'] has the ID number from the uploads table I need to add type from that table a's well but the rest of my table rows are called from a nother database table

Link to comment
Share on other sites

There are several ways to go about this, but the simplest would be to store the paths to the images in the database. The ID alone is not going to do you any good. The ID can help you possibly ID the images you want to display, but you can't actually display them using the ID.

 

Anyway, if you have the image path stored in the DB, then you can simply use that path for the src attribute of the image. For example:

 

DB

Table: images
id   path
1    img/blowfish.jpg
2    img/rainbow.jpg
3    img/winter.jpg

 

And your query might look like the following:

 

$q = "SELECT path FROM images WHERE id = 3";

$r = mysqli_query($dbc, $q);

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) {

 echo '<img src="' . $row['path'] . '">';

}

 

Again, I have greatly simplified things, but that's the general idea. Also, I believe Larry talks about this in detail in the book. Please check out the appropriate chapter.

Link to comment
Share on other sites

Yeah I've looked through that chapter but my setup is different and I don't want to show the images in another window but in a table row inside a table this is what I got but I'm stuck on getting it to display the image still ...

 

in my uploads table the image is stored as a mysqli_insert_id when it is uploaded.

this is my upload table

 

imageid int(10) unsigned not null auto_increment

image_name varchar(255) not null

image_type varchar(6) not null

primary key (imageid)

 

now inside my while loop for the table I have this, I have cut out all the other table rows because they all work.

 

$dir = 'D:/phpul/';

echo '<td align="left">';

$queryi = "SELECT image_name FROM uploads WHERE (imageid='$row[image1]')";

$resulti = mysqli_query ($dbc, $queryi);

$rowi = mysqli_fetch_array($resulti, MYSQLI_ASSOC);

$imageid = $row['image1'];

$imagename = $rowi['image_name'];

echo '<img src="' . $dir . $imagename . '"/></td></tr>';

mysqli_free_result ($resulti);

}

echo '</table>';

mysqli_free_result ($result);

mysqli_close($dbc);

 

what I need to do is take that imageid from my first query that returns the indexes for the other table rows and call the image by its ID for every row then convert it to a workable image source using the image query $queryi, so far it just shows the icon for an image type inside its colum on the table row, I know it would be easier to just store the image in the upload directory as its original file name without mysqli_insert_id because it would be saved in its proper type but then it will be replaced by another other file with the same name so I need to use mysqli_insert_id. when I debug it, it shows it is getting the file name from the $queryi now how can I use that in the <img src=""/> when its name is only stored in the database but the file in the upload directory is just an integer without an extension?

 

this is part of the script that adds the file to the uploads table

 

if (isset($_FILES['i1']) && ($_FILES['i1']['error'] != 4)) {

$queryi1 = "INSERT INTO uploads (image_name, image_type) VALUES ('{$_FILES['i1']['name']}', '{$_FILES['i1']['type']}')";

$resulti1 = mysqli_query ($dbc, $queryi1);

if ($resulti1) {

$idi1 = mysqli_insert_id($dbc);

move_uploaded_file($_FILES['i1']['tmp_name'], "D:/phpul/$idi1");

}

} else {

$idi1 = "NULL";

}

 

there are 6 more like this because the script takes upto 6 uploads.

 

but in the script I'm working on the page only needs to display the first image added but these 6 values are stored in the main table as $idi1 through $idi6 as image1 through image6 which you can see is how I get the imageid in my first query for $row['image1'] which returns as an integer referring to the file in the uploads directory as that same integer.

 

would it be simpler to make a tempory file out of the one that is stored in the uploads directory if that is possible?

Link to comment
Share on other sites

Dark Prince, I'll be honest: I can barely understand what you're saying or trying to do...but I want to help you.

 

First, I have a few questions:

1) Are you working in a Windows environment? If so, you need to use backslashes, not forward slashes, for $dir.

2) The field names in the DB really give me no clue as to possible values. For example, is image_name the arbitrary name you've given a file, or the actual file name with extension?

 

Please provide some specific examples of the data in your DB as well as how that corresponds to actual images in a directory. From there, I can better help you.

Link to comment
Share on other sites

yes image_name is the original file name with extension.

 

and yeah I'm on xp home and the and I believe I am using forward slashes because backslashes don't work in windows? because all my other scripts using uploads use forward slashes and work like these /, what I need to do is turn the imageid from the databases main table which fills the rest of the page table, the image id is stored in that table now that id links to the image id in the uploads table that has the name with extension and the type of file that it is. so I need to take the image id from the databases main table to display the image from the uploads directory using the image information in the uploads table.

 

so in my uploads directory everthing is stored as a unique integer with no extensions or anything there information is stored in the uploads table with again a unique integer for an id that is stored to its corresponding row in the databases main table.

Link to comment
Share on other sites

Are you using a foreign key to link the tables? If not, you should be.

 

Basically, the id field of one table should be a foreign key in the other table, so that you can perform joins across tables, and get all the info you need in one query. That's the heart and soul of relational database management systems (like MySQL).

 

If you're not doing that, you need to reconfigure your database accordingly, and then set up the proper joins in your queries. Check out the MySQL chapter (especially the part about joins) for details.

 

Once you get the query right, it's just a matter of plugging in the right path and file name for the src attribute of the img tags.

 

Also, I'm not actually sure how you're getting content from local directory paths via a PHP script. The only time you have access to local directory paths is for input fields in a form with a Browse button.

 

For the actually displaying of the images, you need to be referencing images on your (virtual) server.

Link to comment
Share on other sites

The main problem is when the file is uploaded I use mysqli_insert_id so a file with the same name as another can be uploaded so all my uploaded files are files without extension or name, that is stored in the upload table

 

 

So if there is no easy way to do this, how would I take that raw file out of my uploads directory and load it as a temporary file with the file name from the uploads table then display that tempory file as the image source?

I gave my 2nd edition book to my friend and I used the file upload system from that books examples.

now in the 3rd edition it doesn't have the same example where it takes multiple file uploads at once and stores them as a mysql id in the directory then the name in the uploads table.

Link to comment
Share on other sites

Dark Prince, I'm still confused what you're talking about. For one, ALL database tables should have at least one field that is unique for every record (and hopefully the values in that unique field never change as well). If you don't have that, you need to redesign your DB.

 

Also, I do not understand how you can have two files with the same name in the same directory and not have one overwrite the other.

 

I don't know what your level of experience is, but I highly recommend going through Larry's examples in the book, and then reworking the code to fit your needs, because (while admittedly unclear), it seems like you're taking the wrong approach, and will never accomplish what you want.

Link to comment
Share on other sites

Ok I'll try to explain this without making it nonsense... first the ID is unique for all the files uploaded and that ID is also the files new name in the uploads directory so it is a raw file with its ID as its name, now its actual name with extension is stored in the uploads database table. now the rest of the information that is not a file is stored in a main table with its files ID's that were uploaded with it so when I call a row I can see which files were uploaded to that row. so I'm displaying all that information in an html table so everything is displayed fine. But I need to take the first files ID in everyrow and display it as a thumbnail in the html table but every table row is going to have a different file id because its uploaded files are different from the next row in the database(like on this site if you goto members it will show all the members and the second column is a picture) i'm trying to do something very similar except the files are stored as its ID not as the original file that way a file that has the same name as another can exist in the same upload directory.

 

why in my while loop for the images row I do the files information query then clear the result so the next row in the loop can call the information for its own files.

 

but because the files name is changed to its ID I need to take that file and make it a temporary file with its orginal name and extension so it can be called like that in the <img src="$file"> so I need $file to be defined as the original file using the raw file in the uploads directory and the files original name and extension from the uploads table in the database, now in Larry's second edition he had a script that called the that raw file which was just an ID for a name and used the information entered into the database for it to download the file in its original form. I don't want to download the file I want to display it in a html table.

Link to comment
Share on other sites

This is the script with row names removed to protect my friends information for the site

 

$dir = "D:/phpul/";

// Query.

$query = "SELECT row1, row2, row3, row4, image1, DATE_FORMAT(datetime, '%M %d, %Y') as dr, rowid FROM table ORDER BY $order_by LIMIT $start, $display";

$result = mysqli_query ($dbc, $query);

?>

<br/>

<div align="center"><h2><p>Table Title</p></h2></div>

<?php

// Table header.

echo '

<fieldset id="fieldset1">

<table align="center" cellspacing="10" cellpadding="10" width="90%">

<tr>

<td align="center"><b><a href="index.php?sort=ast">row1 name</a></b></td>

<td align="center"><b>Photo</b></td>

<td align="center"><b>row2 name</b></td>

<td align="center"><b><a href="index.php?sort=asp">row3 name</a></b></td>

<td align="center"><b><a href="index.php?sort=asl">row4 name</a></b></td>

<td align="center"><b><a href="index.php?sort=asr">date added to database</a></b></td>

</tr>

';

// Fetch and print table rows.

$bg = '#eeeeee'; // initial bg color.

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');

echo '<tr bgcolor="' . $bg . '">

<td align="center"><a href="somepage.php?id=' . $row['rowid'] . '">' . $row['row1'] . '</a></td>

<td align="center">';

$queryi = "SELECT image_name FROM uploads WHERE (imageid='$row[image1]')";

$resulti = mysqli_query ($dbc, $queryi);

$rowi = mysqli_fetch_array($resulti, MYSQLI_ASSOC);

$imageid = $row['image1'];

$imagename = $rowi['image_name'];

echo '<img src="' . $dir . '' .$imagename . '"/></td> (now this obviously doesn't work because that file is stored without a name in the upload directory so shows as an unavailable picture aka the picture icon for IE)

<td align="center">' . $row['row2'] . '</td>

<td align="center">' . $row['row3'] . '</td>

<td align="center">' . $row['row4'] . '</td>

<td align="center">' . $row['dr'] . '</td>

</tr>

';

mysqli_free_result ($resulti);

}

echo '</table></fieldset>';

mysqli_free_result ($result);

mysqli_close($dbc);

 

 

now the script that handles the file uploads is this. (this takes the filename with extension and type then stores it in the uploads table. Then it changes the filename to and ID number without an extension and moves it to the uploads directory but that name is the same as its ID in the uploads table.

 

if (isset($_FILES['i1']) && ($_FILES['i1']['error'] != 4)) {

$queryi1 = "INSERT INTO uploads (image_name, image_type) VALUES ('{$_FILES['i1']['name']}', '{$_FILES['i1']['type']}')";

$resulti1 = mysqli_query ($dbc, $queryi1);

if ($resulti1) {

$idi1 = mysqli_insert_id($dbc);

move_uploaded_file($_FILES['i1']['tmp_name'], "D:/phpul/$idi1");

}

} else {

$idi1 = "NULL";

}

if (isset($_FILES['i2']) && ($_FILES['i2']['error'] != 4)) {

$queryi2 = "INSERT INTO uploads (image_name, image_type) VALUES ('{$_FILES['i2']['name']}', '{$_FILES['i2']['type']}')";

$resulti2 = mysqli_query ($dbc, $queryi2);

if ($resulti2) {

$idi2 = mysqli_insert_id ($dbc);

move_uploaded_file($_FILES['i2']['tmp_name'], "D:/phpul/$idi2");

}

} else {

$idi2 = "NULL";

}

if (isset($_FILES['i3']) && ($_FILES['i3']['error'] != 4)) {

$queryi3 = "INSERT INTO uploads (image_name, image_type) VALUES ('{$_FILES['i3']['name']}', '{$_FILES['i3']['type']}')";

$resulti3 = mysqli_query ($dbc, $queryi3);

if ($resulti3) {

$idi3 = mysqli_insert_id ($dbc);

move_uploaded_file($_FILES['i3']['tmp_name'], "D:/phpul/$idi3");

}

} else {

$idi3 = "NULL";

}

if (isset($_FILES['i4']) && ($_FILES['i4']['error'] != 4)) {

$queryi4 = "INSERT INTO uploads (image_name, image_type) VALUES ('{$_FILES['i4']['name']}', '{$_FILES['i4']['type']}')";

$resulti4 = mysqli_query ($dbc, $queryi4);

if ($resulti4) {

$idi4 = mysqli_insert_id ($dbc);

move_uploaded_file($_FILES['i4']['tmp_name'], "D:/phpul/$idi4");

}

} else {

$idi4 = "NULL";

}

if (isset($_FILES['i5']) && ($_FILES['i5']['error'] != 4)) {

$queryi5 = "INSERT INTO uploads (image_name, image_type) VALUES ('{$_FILES['i5']['name']}', '{$_FILES['i5']['type']}')";

$resulti5 = mysqli_query ($dbc, $queryi5);

if ($resulti5) {

$idi5 = mysqli_insert_id ($dbc);

move_uploaded_file($_FILES['i5']['tmp_name'], "D:/phpul/$idi5");

}

} else {

$idi5 = "NULL";

}

if (isset($_FILES['i6']) && ($_FILES['i6']['error'] != 4)) {

$queryi6 = "INSERT INTO uploads (image_name, image_type) VALUES ('{$_FILES['i6']['name']}', '{$_FILES['i6']['type']}')";

$resulti6 = mysqli_query ($dbc, $queryi6);

if ($resulti6) {

$idi6 = mysqli_insert_id ($dbc);

move_uploaded_file($_FILES['i6']['tmp_name'], "D:/phpul/$idi6");

}

} else {

$idi6 = "NULL";

}

 

now $idi1 through $idi6 which are now mysql id's and primary keys are stored in the main table in the database so they can be distinquished to that INSERT only and there information from the uploads table can be linked to them for the main tables primary key

 

but the columns name are not foreign to each other because the main table can take 6 image id's for 1 row so the rest of the query in the submit page looks like this

 

$query = "INSERT INTO sometable (somecolumn, somecolumn, somecolumn, somecolumn, somecolumn, somecolumn, somecolumn, somecolumn, somecolumn, some column, image1, image2, image3, image4, image5, image6, datetime) VALUES ('$definedvalue', '$definedvalue', '$definedvalue', '$definedvalue', '$definedvalue', '$definedvalue', '$definedvalue', '$definedvalue', '$definevalue', '$definedvalue', '$idi1', '$idi2', '$idi3', '$idi4', '$idi5', '$idi6', NOW() )";

$result = mysqli_query ($dbc, $query);

 

now somecolumn isn't whats actually in the code they are the column names in the main table and same with $definedvalue they are values taken from form fields.

 

hope this can help clear up more as to what I'm working with and trying to do.

Link to comment
Share on other sites

Okay, I have never read Larry's 2nd edition book, and I don't have the 3rd edition book in front of me (at work), but I can almost guarantee that he doesn't handle image/file uploads like your script.

 

Whatever you do, don't take the file extension off of the file. Also, if you're worried about file names overlapping, then you'll need to create subfolders when a file is uploaded. This can be easily done via the PHP mkdir function (http://php.net/manual/en/function.mkdir.php). If you need an ID for the images, either use the auto-incrementing primary key for the images table, or add an extra field to the images table with your own ID, but whatever you do, don't change the file name (and definitely do not delete the extension).

 

That should be your first step. If you do that properly, then you should at least be able to display the images properly. And instead of posting long amounts of code, please just follow the code in Larry's book, and modify it as necessary. If you lost the book or whatever, find it or buy another copy. You can also buy an electronic copy for a reduced price.

 

The second big no-no is making another query each time through the while loop. This is incredibly inefficient and slow. Don't ever do it, please!

 

Instead, you need to connect your images table to your main table via a foreign key, as I mentioned before. Basically, the primary key in the images table for each image should correspond to a record in the main table and be stored there as a foreign key. You can then perform a join to get all the necessary data in one query.

 

Joins are admittedly difficult, so please look over the section about joins in the book and try them out before complaining here that you can't do them. If you absolutely cannot do it, then myself (or someone else on the forum) will gladly show you how to create the specify join you need.

 

Anyway, I don't mean to sound rude (I want to help), but you really need to take a step back and rethink things (and while you're rethinking, please, please look over and practice the relevant sections in Larry's book). After you restructure everything and have it more or less working, the people on this forum will probably be more than willing to help you with specific issues you are having.

 

I imagine that's not the answer you wanted to hear, but the truth is that if you want this to work properly and learn something from it, you need to take some more time to study and learn things properly.

 

Lemme know if you have any additional questions. Thank you.

Link to comment
Share on other sites

In addition to the excellent advise from HartleySan, there is a problem with how you are referencing the file after making your query:

$rowi = mysqli_fetch_array($resulti, MYSQLI_ASSOC);

$imageid = $row['image1'];

$imagename = $rowi['image_name'];

Notice that you fetch the results of your query into $rowi but you are referencing $row (no i) for $imageid and $imagename. That's not going to work...

Link to comment
Share on other sites

yes because $row is the first query for the loop the $rowi is for the images information in the uploads directory. and I think larry used a loop to do the file uploads the way I did I just took the loop out and did each one by itself.

 

Making sub directories was going to my next step if this wouldn't work some reason I make things more complicated then they should be lol sorry guys I'll see what I can do to re-write this.

Link to comment
Share on other sites

ugh ok so I looked through the scripts from 2nd edition glad I still had those, the way larry does it is he sends them into another window to be downloaded via the header function :(

 

// Check for an upload_id.

if (isset($_GET['uid'])) {

$uid = (int) $_GET['uid'];

} else { // Big problem!

$uid = 0;

}

if ($uid > 0) { // Do not proceed!

require_once ('../mysql_connect.php'); // Connect to the database.

// Get the information for this file.

$query = "SELECT file_name, file_type, file_size FROM uploads WHERE upload_id=$uid";

$result = mysql_query ($query);

list ($fn, $ft, $fs) = mysql_fetch_array ($result, MYSQL_NUM);

mysql_close(); // Close the database connection.

// Determine the file name on the server.

$the_file = '../uploads/' . $uid;

 

// Check if it exists.

if (file_exists ($the_file)) {

 

// Send the file.

header ("Content-Type: $ft\n");

header ("Content-disposition: attachment; filename=\"$fn\"\n");

header ("Content-Length: $fs\n");

readfile ($the_file);

Link to comment
Share on other sites

Dark Prince, I recommend taking this one piece at a time, testing, and then going onto the next step.

 

For example, start out by manually placing some images in a server directory, and then placing the info about those images in a database table. From there, try running a basic script to acquire that DB info and display the images.

 

Once that's okay, continue to build up your image viewer table stats, little by little, testing all the while.

 

You may need multiple tables to normalize everything, in which case, joins will become necessary, so be sure to test that out as well.

 

Finally, when the image viewer aspect is working fine with manually adding images, I would then (as a last step), add the ability to upload images and auto-register them in the DB.

 

What do you think?

Link to comment
Share on other sites

I've given up on the complicated but easy way, I'm just going to use folders using user ids and page ids for the correlating images.

 

So far I changed the signup page where people will create a username and password so if the query runs ok the mkdir function makes a directory with there userid inside the uploads folder.

Link to comment
Share on other sites

That's a good start.

 

Like I said before, I would recommend manually placing image entries into the DB for now to teat things out.

 

Then, once your SQL queries are solid, you can start automizing things.

 

Glad you're taking it slow, and one logical step at a time though.

Link to comment
Share on other sites

Ok so I've had some success inside the row it is still only showing an image icon. when I click on the properties of the image it shows the name with extension but the rest of the info is empty, this is the new code

 

the $dir value is set like this $dir = 'D:/phpul';

the image1 row in the database is the files name with extension now and the folder it is stored in is the same as the userid so with the code below the browser should show it as <img src="D:/phpul/1/2.jpg" width="75" height="75"/>

but it does not? I've checked the folder itself the file is there in the userid folder in the phpul directory.

 

<td align="center"><img src="'.$dir.'/'.$row['userid'].'/'.$row['image1'].'" width="75" height="75"/></td>

 

I know i'm dragging this topic on quite a bit but I'm doing this for a friend out my own good will.

Link to comment
Share on other sites

It's cool. I don't mind going through it one step at a time.

 

As far as I know (and I could be wrong), it is not possible to access a file stored locally on your hard drive. You need to have the images on the server. Upload the images to a folder on the server, and you should be able to access them fine.

 

Like I said before, do everything manually at first to confirm your queries and whatnot, and then create the upload script.

Link to comment
Share on other sites

so pretty much need to take the file from the uploads directory and create it as a temp in the htdocs folder? I have everything to do with php mysql and apache on a partition called D:/ so my phptemp and phpul is just outside the xampp folder

D:\

|-phptmp

|-phpul/userid/file.ext

|-xampp/htdocs/sitefolder/script.php

 

thats the directory setup best way I can show it.

Link to comment
Share on other sites

Ok I moved the file into a folder inside the htdocs folder still same thing happening if I echo it instead of using img src= it shows the full pathname and file with extension but still shows the picture icon, it doesn't show a broken link icon though which wasn't the case anyways.

Link to comment
Share on other sites

 Share

×
×
  • Create New...