hello guys how are i have a proplem with upload files
when i tring to uploading images from my script on the server it is not display all images that i uploaded althoug i upload for instance 50 images and when the upload finishe it will just display about 24 images

but on my local host it work's normaly if i upload 50 images it will display 50 images

this problem is just accur on my server
this is my code form uploading images

<?php @session_start();?>
<html>
</head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="admin.css" media="screen"/>
</head>
<body>

<?







?>


<?php 

mysql_connect('localhost','root','root')or die (mysql_error());
mysql_select_db('storeimage');

?>

<?php 





$allowedExt  = array("jpeg","JPEG","jpg","JPG");
if (isset($_FILES['file'])) {
    foreach ( $_FILES['file']['tmp_name'] as $key => $file ) {
        if ($_FILES["file"]["error"][$key] == UPLOAD_ERR_OK) {
            $fileName = $_FILES['file']['name'][$key];
            $fileSize = $_FILES['file']['size'][$key];
            $fileTemp = $_FILES['file']['tmp_name'][$key];
            $fileType = $_FILES["file"]["type"][$key];
            $fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
            $fileExt = strtolower($fileExt);
            $randname=rand().'.jpeg'; 
            if(!in_array($fileExt, $allowedExt))
            {
                // extention not supported ;
                echo "Extension Not Allowed";
                continue;
            }

            else {
            move_uploaded_file($fileTemp,'images/'.$randname);

            $insert=mysql_query("insert into  imagelocation values('','images/$randname')")or die (mysql_error());
            $id=mysql_insert_id();
            $select=mysql_query("select * from  imagelocation where id='$id' ");

            while ($row=mysql_fetch_object($select)){
            echo "<center><img  src='$row->images'/></center><br/><br/>";
            }


            }




        } else {
            // Somethign bad happend 
            echo "please choose the files";
        }
    }
}







?>


<form action='' method='post' enctype='multipart/form-data'>
<input type='file' name='file[]' multiple />
<input type='submit' name='submit'/>

</form>


<?php 
if ($_SESSION['username']){
echo "<a href='logout.php'><div id='welcome'>logout</div></a>";

}
else{die ("<meta http-equiv='refresh' content='0; url=login.php'/>");}



?>

</body>
</html>

Recommended Answers

All 24 Replies

Are you getting any errors when uploading the files either on screen or in the server error log?

Do the images you're trying to upload exceed the filesize limit?

Are the 50 images appearing in the database?

Are the 50 images being successfully uploaded into your images directory?

Also consider you can get some collisions with rand() and overwrite previously existing files. Add a check for existing files and, in that case, rename the new one again.

Are you getting any errors when uploading the files either on screen or in the server error log?

  • i do not getting any error

Do the images you're trying to upload exceed the filesize limit?

  • no because i am not specify a limit for images

Are the 50 images appearing in the database?

  • no just 24 images as i get when i finish uploaind images

Are the 50 images being successfully uploaded into your images directory?

  • alose just 24

Also consider you can get some collisions with rand() and overwrite previously existing files. Add a check for existing files and, in that case, rename the new one again.

but as i know rand() does not repeating the number is it right ?

Member Avatar for diafol

I just wonder why you're using rand. Can't you use a microtimestamp with increment or something? Something that's pretty much guaranteed not to have a collision.

hello diafol do you mean as this date("d m Y,G i s A").'.jpeg'; this will generate
15 12 2012,10:57:09 AM ? it does not work's my uploading images is just upload one image by this way
because secouds are the same by the time i uploading images
now i am working on my localhost not on my server

now i will try to use microtime() and i will tell you what will happen

also i have used $randname =microtime().rand().'.jpeg'; but it is the same result
i think the cuase of this problem the string is generate the same number

Member Avatar for diafol

I mentioned using an incrementer:

$x = 1;
list($usec, $sec) = explode(" ", microtime()); 
$fileTime = $sec . substr($usec,2); //concatenates and gets rid of decimal point

//for each image uploaded (I assume in an array of $_FILES)
foreach ($img as $i){
    ...
    $fileName = $fileTime . $x . $fileExt;
    ...
    $x++;
}

I have to admit this isn't a very nice solution. I only included the microtime as I thought you may have more than one user uploading at the same second. If this is not the case, you can just use time(). ALso if an user has an user_id - that can make a part of the filename, so microtime can be done away with.

no there is no one can upload just admin who can upload images
and i have tried to use time() but i get the same result and by this way all images are repeated and in directory there is just one image

now i am trying to use your last code and i ill tell you what will happen
your last code is work normally on localhost
and now i will try it on the server

i have used your last code and i upload 30 images but it display just 20 images but on localhost it work's normally

??

If using a unique filename doesn't resolve the issue, you'll need to debug your code at each stage. To do this, you could write the upload progress for each image to a log file. E.g.
- Image uploaded (to temporary directory)
- Image extension allowed / disallowed
- Image filename
- Image moved to images directory
- SQL query
- Image inserted into database

This would then allow you to see where it's failing for each.

code after adjustment

<?php @session_start();?>
<html>
</head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="admin.css" media="screen"/>
</head>
<body>

<?







?>


<?php 

mysql_connect('localhost','ali_root','kingstone2012')or die (mysql_error());
mysql_select_db('ali_storeimage');

?>

<?php 





$allowedExt  = array("jpeg","JPEG","jpg","JPG");
if (isset($_FILES['file'])) {

$x = 1;
    list($usec, $sec) = explode(" ", microtime());
    $fileTime = $sec . substr($usec,2); //concatenates and gets rid of decimal point
    //for each image uploaded (I assume in an array of $_FILES)


    foreach ( $_FILES['file']['tmp_name'] as $key => $file ) {
        if ($_FILES["file"]["error"][$key] == UPLOAD_ERR_OK) {
            $fileName = $_FILES['file']['name'][$key];
            $fileSize = $_FILES['file']['size'][$key];
            $fileTemp = $_FILES['file']['tmp_name'][$key];
            $fileType = $_FILES["file"]["type"][$key];
            $fileExt = pathinfo($fileName, PATHINFO_EXTENSION);
            $fileExt = strtolower($fileExt);
            //$randname =microtime().rand().'.jpeg';
            $randname = $fileTime . $x .'.'.$fileExt;   


             $x++;
            if(!in_array($fileExt, $allowedExt))
            {
                // extention not supported ;
                echo "Extension Not Allowed";
                continue;
            }

            else {
            move_uploaded_file($fileTemp,'images/'.$randname);

            $insert=mysql_query("insert into  imagelocation values('','images/$randname')")or die (mysql_error());
            $id=mysql_insert_id();
            $select=mysql_query("select * from  imagelocation where id='$id' ");

            while ($row=mysql_fetch_object($select)){
            echo "<center><img  src='$row->images'/></center><br/><br/>";
            }


            }




        } else {
            // Somethign bad happend 
            echo "please choose the files";
        }
    }
}







?>


<form action='' method='post' enctype='multipart/form-data'>
<input type='file' name='file[]' multiple />
<input type='submit' name='submit'/>

</form>


<?php 
if ($_SESSION['username']){
echo "<a href='logout.php'><div id='welcome'>logout</div></a>";

}
else{die ("<meta http-equiv='refresh' content='0; url=login.php'/>");}



?>

</body>
</html>

blocblue how can i do this, i do not have the capability to do everything in php

Member Avatar for diafol
if(isset($_POST['submit'])){

    $x = 1;
    $fname = time();
    $uploadDir = "uploads/pics";

    foreach ($_FILES["file"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $oldFN = $_FILES["file"]["tmp_name"][$key];
            $ext = '.' . pathinfo($_FILES["file"]["name"][$key], PATHINFO_EXTENSION);
            $newFN = $fname . $x . $ext;
            move_uploaded_file($oldFN, "$uploadDir/$newFN");
            //code to add to a DB record or concatenate the value to place in a single query after this
            $x++;
        }
    }
}

That's mangled from the php manual

hello diafol also i get the same result

<?php @session_start();?>
<html>
</head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="admin.css" media="screen"/>
</head>
<body>

<?







?>


<?php 

mysql_connect('localhost','root','root')or die (mysql_error());
mysql_select_db('storeimage');

?>

<?php 





if(isset($_POST['submit'])){

    $x = 1;
    $fname = time();
    $uploadDir = "images/";

    foreach ($_FILES["file"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $oldFN = $_FILES["file"]["tmp_name"][$key];
            $ext = '.' . pathinfo($_FILES["file"]["name"][$key], PATHINFO_EXTENSION);
            $newFN = $fname . $x . $ext;
            move_uploaded_file($oldFN, "$uploadDir $newFN");

            //code to add to a DB record or concatenate the value to place in a single query after this
    $insert=mysql_query("insert into  imagelocation values('','$uploadDir $newFN')")or die (mysql_error());
            $id=mysql_insert_id();
            $select=mysql_query("select * from  imagelocation where id='$id' ");

            while ($row=mysql_fetch_object($select)){
            echo "<center><img  src='$row->images'/></center><br/><br/>";
            }


            $x++;
        }
    }
}


?>


<form action='' method='post' enctype='multipart/form-data'>
<input type='file' name='file[]' multiple />
<input type='submit' name='submit'/>

</form>


<?php 
if ($_SESSION['username']){
echo "<a href='logout.php'><div id='welcome'>logout</div></a>";

}
else{die ("<meta http-equiv='refresh' content='0; url=login.php'/>");}



?>

</body>
</html>
Member Avatar for diafol

Which is what? You get 20 images entered but not the full 30?

This sounds as though it may be due to max filesize.

i mean when i uploading for instance 30 images will display just 20 images on the screen this will happen on the server but on my localhost host it work's normally all images that i will uploaded will display

should i specify the maxfilesize ? do you think that is the cause of the problem ?

Member Avatar for diafol

You need to see the max file uploads under CORE on the server compared to your localhost.

Use phpinfo() to see the value. It's set to 20 on my pc. I think you'll need to change php.ini. But if you're on a shared host, you probably won't be allowed to do this.

yes diafol i am on sharing host and on my server the max file uploads is 20

Member Avatar for diafol

There you go then.

i would like to thank all guys who help me especially diafol i appreciate you hard work
problem has been solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.