0

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>
4
Contributors
24
Replies
26
Views
4 Years
Discussion Span
Last Post by diafol
Featured 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? Read More

  • 1
    diafol 3,720   4 Years Ago

    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. Read More

  • 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 … Read More

  • 1
    diafol 3,720   4 Years Ago

    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 … Read More

1

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?

0

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.

0

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

Edited by cssweb

0

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 ?

1

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.

0

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

Edited by cssweb

0

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

0

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

Edited by cssweb

0

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.

0

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

Edited by cssweb

0

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

0

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

??

1

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.

0

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>
0

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

0
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

0

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>

Edited by cssweb

0

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

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

0

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 ?

Edited by cssweb

1

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.

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.