jbegreen Posted May 31, 2013 Share Posted May 31, 2013 Hey Wondered if anyone could help me with this. I had this right a couple of days ago but for some reason it wasn't in my code yesterday and since rebuilding, has ceased to work. I have a multi file upload that creates a directory as the name of the username variable ($username_entry). When using this variable in the file path along with the variable for the name of the file ($name) the file path simply puts the variable text "$name" as some random blank file within the newly created directory. Im sure it something pretty small that needs editing to use 2 variables in 1 path. Anyway here's the code i need tweaking. Thanks to anyone that can help me <?php if (isset($_FILES['upload']) === true) { $files = $_FILES['upload']; for($x = 0; $x < count($files['name']); $x++) { $name = $files['name'][$x]; $tmp_name = $files['tmp_name'][$x]; move_uploaded_file($tmp_name, 'user_images/'.$username_entry.'/ . $name'); } ?> Its the move_uploaded_file($tmp_name, 'user_images/'.$username_entry.'/ . $name'); where i think the problem lies.......any takers? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/ Share on other sites More sharing options...
Solution DaveyK Posted May 31, 2013 Solution Share Posted May 31, 2013 (edited) Try echoing the variables without actually moving the uploaded file. What do you get? <?php echo '<pre>' . print_r($_FILES, true) . '</pre>'; ?> Also, try something like this: move_uploaded_file($tmp_name, "user_images/{$username_entry}/{$name}"); Edited May 31, 2013 by DaveyK Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433373 Share on other sites More sharing options...
jbegreen Posted May 31, 2013 Author Share Posted May 31, 2013 Array( [upload] => Array ( [name] => Array ( [0] => test.jpg [1] => facebook_cover_small copy.jpg [2] => [3] => [4] => ) [type] => Array ( [0] => image/jpeg [1] => image/jpeg [2] => [3] => [4] => ) [tmp_name] => Array ( [0] => C:\WINDOWS\Temp\php93B.tmp [1] => C:\WINDOWS\Temp\php93C.tmp [2] => [3] => [4] => ) [error] => Array ( [0] => 0 [1] => 0 [2] => 4 [3] => 4 [4] => 4 ) [size] => Array ( [0] => 595377 [1] => 60665 [2] => 0 [3] => 0 [4] => 0 ) )) There are 5 files possible to upload. Used 2 to demonstrate print r. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433374 Share on other sites More sharing options...
jbegreen Posted May 31, 2013 Author Share Posted May 31, 2013 Thanks DaveyK it was the curly braces! Although i didn't use these last time. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433379 Share on other sites More sharing options...
DaveyK Posted May 31, 2013 Share Posted May 31, 2013 (edited) I dont see why this wouldnt work: <?php $files = $_FILES['upload']; foreach ($files['name'] as $k => $file) { var_dump(move_uploaded_file($files['tmp_name'][$k], "user_images/{$username_entry}/{$file}")); } what does that do? EDIT: Oh, well in that case just ignore this post. You are welcome. It actually wasnt just the curly brackets. You made a typo here: move_uploaded_file($tmp_name, 'user_images/'.$username_entry.'/'. $name); You missed the red one. Edited May 31, 2013 by DaveyK Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433380 Share on other sites More sharing options...
jbegreen Posted May 31, 2013 Author Share Posted May 31, 2013 for the array count. Is there any easy way of selecting the file path (with variables) for each count and inserting into separate fields in a MySQL database? Just thought i would throw that out there! Cheers Jack Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433381 Share on other sites More sharing options...
DaveyK Posted May 31, 2013 Share Posted May 31, 2013 (edited) Yes, you can use mysqli() or PDO_mysql() to prepare a statement PRIOR to the for loop, and then you bind the param inside the loop and execute the query dont use mysql_*, always use mysqli() or PDO_mysql for php libraries if that is an option. HERE is an example for pdo: $pdo = new PDO (); $sql = "INSERT INTO ................"; $stmt = $pdo->prepare($sql); for () { $stmt->bindParam(':file_name', $name); $stmt->execute(); } this WONT work but will give you a general idea about what it might look like Edited May 31, 2013 by DaveyK Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433382 Share on other sites More sharing options...
jbegreen Posted May 31, 2013 Author Share Posted May 31, 2013 Yes! HaHa! I knew it was something like that! I appreciate your time. Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433383 Share on other sites More sharing options...
jbegreen Posted May 31, 2013 Author Share Posted May 31, 2013 Is there anyway you could go into more detail with the PDO. Not very familiar with it. THanks so much. J Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433387 Share on other sites More sharing options...
DaveyK Posted May 31, 2013 Share Posted May 31, 2013 Sure. I am fairly new to PDO as well, so it might be wrong what I say here, but here it goes. PDO is a php library for connecting to a mysql database. That means it is OOP. You create a PDO connection by doing this code: $pdo = new PDO('mysql:host=HOST;dbname=DB_NAME;charset=utf8', USER_NAME, PASSWORD, array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" )); $pdo->exec("set names utf8"); Just replace the CAPS by whatever is relevant to your site. HOST is like an IP address and DB_NAME is the database name and so forth. I am using an UTF-8 (Unicode) charset so I went overboard making sure verything is utf-8. You can remove that part if you want to. Say you have some query, like: SELECT * FROM users WHERE users.id = $id In PDO, you can do $pdo->prepare('SELECT * FROM users WHERE users.id = :id'); $pdo->bindParam(':id', $id); $pdo->execute(); $result = $pdo->fetch(); // this gives a single array Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/#findComment-1433399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.