Jump to content

Small code - small problem - can't find it!


Go to solution Solved by DaveyK,

Recommended Posts

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. :)
Link to comment
https://forums.phpfreaks.com/topic/278632-small-code-small-problem-cant-find-it/
Share on other sites

  • Solution

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 by DaveyK
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. :)

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 by DaveyK

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 by DaveyK

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.