Blank Array Indecies?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Blank Array Indecies?

Post by IceMetalPunk »

*PROBLEM SOLVED, BUT THERE'S ANOTHER PROBLEM! SCROLL DOWN :) *

-IMP ;) :)
Last edited by IceMetalPunk on Wed Jul 20, 2005 8:31 pm, edited 4 times in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Do you mean a file someone has uploaded when you say "a file online"? Coz if you do, don't rely on the extension.. might be a Mac or a Unix user, their files don't necessarily have extensions in the file name. Instead, use $_FILES['userfile']['type'] to get the MIME type:

http://uk.php.net/features.file-upload

Each image will be one of:

GIF: image/gif
JPG: image/jpeg
PNG: image/png

Alternatively, calling getimagesize() returns the type along with other info.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Nope, not an uploaded file, just a URL. But I found the problem: When converting the URLs into an array, some end up blank.... maybe someone can help me figure out why? Here's the relevant (sp?) code:

Code: Select all

$info=file_get_contents("mri".$uid.".txt");
$info=explode("\r\n",$info);
$w=$info[0];
$h=$info[1];
$num=$info[2];
$pwd=$info[3];
$urls="";
for ($i=4; $i<count($info); $i+=1) {
$urls.=$info[$i]."\r\n";
}
$urls=explode("\r\n",$urls);
for ($i=0; $i<$num; $i+=1) {
$imgs[$i]=$urls[rand(0,count($urls)-1)];
}
All the other info is stored okay, but when I convert it to the urls array, even though there are only 3 URLs after the 4th line in the file, I end up with an array like this (this is a pseudo-array):

Code: Select all

[0]=>URL1
[1]=>URL2
[2]=>URL3
[3]=>
[4]=>
Notice the 2 blank spots at the end? I don't know why they're appearing, but they're messing everything up, and as there may be more than 3 URLs later, I can't just tell it to stop at array index 2.

-IMP ;) :)
Post Reply