Hack Forums
[PHP] Strict Standards!? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Strict Standards!? (/showthread.php?tid=2611140)



Strict Standards!? - DAVEologist - Jun 22, 2012

Darn! I always get this error.

Code
Strict Standards: Only variables should be passed by reference in C:\XamPP\htdocs\workspace\dawnsite\test.php on line 16

This is the code.

PHP Code

<?php 
        
foreach ($b as $c) {
            
//upload multiple images
            
$d = strtolower(end(explode('.', $_FILES["file"]["name"][$c]))); //the 16th line
            
$d = $d;
            if ((
$d !== 'jpg')
             OR (
$d !== 'jpeg')
             OR (
$d !== 'gif')
             OR (
$d !== 'bmp')
             OR (
$d !== 'png')) {
                echo 
$d;
            }
        } 

$b is an array of numbers.
PHP Code

<?php 
$a 
= count($_FILES['file']['name'])-1;
$b = range(0,$a); 

I am trying to make a multiple image uploader, but it always echoes
$d no matter i use $d == 'jpg' or $d !== 'jpg'

Can someone help me? Php noob here.
BTW, here's the form.

Code
<form action="" method="post" enctype="multipart/form-data" >
<input name="file[]" type="file" multiple id="file" />
<input class="submitimg" value="Upload" type="submit" name="submitimage" />
</form>

Thanks so much. :)


* - badbrains - Jun 22, 2012

there isn't a line 16..
You're trying to pass an instance by reference.
Don't do it. You need PHP 5.4 for that.


* - jQuery - Jun 22, 2012

'end' just points the passed variable to the last element. If you don't create it in the memory (create a variable) it won't work.
Just change your code to
PHP Code

<?php 
$_d 
= explode('.', $_FILES["file"]["name"][$c]);
$d = strtolower(end($_d)); 



* - Love2D - Jun 22, 2012

Well if you're just going to check the file extension, you could use strrchr.

PHP Code

<?php 
$d 
= strrchr(strtolower($_FILES["file"]["name"][$c]), '.'); 



* - Phizo - Jun 22, 2012

(Jun 22, 2012 05:51 PM)Love2D Wrote:  Well if you're just going to check the file extension, you could use strrchr.

PHP Code

<?php 
$d 
= strrchr(strtolower($_FILES["file"]["name"][$c]), '.'); 

Just thought I would point out that there is a case-insensitive way of checking via the stristr() function. I'm not sure if you knew about it. You use strstr() (or strchr()) for case-sensitive, strchr() is just an alias of strstr().

PHP Code

<?php 
$d 
= stristr($_FILE['file']['name'][$c]), '.'); 



* - Love2D - Jun 22, 2012

(Jun 22, 2012 10:54 PM)Phizo Wrote:  Just thought I would point out that there is a case-insensitive way of checking via the stristr() function. I'm not sure if you knew about it. You use strstr() (or strchr()) for case-sensitive, strchr() is just an alias of strstr().

PHP Code

<?php 
$d 
= stristr($_FILE['file']['name'][$c], '.'); 

While that is useful, it won't necessarily help him here. Not to mention the fact that he is looking for a specific character case, and will have to alter his if statement depending on how the user decides to name their file.


* - Phizo - Jun 22, 2012

(Jun 22, 2012 11:14 PM)Love2D Wrote:  While that is useful, it won't necessarily help him here. Not to mention the fact that he is looking for a specific character case, and will have to alter his if statement depending on how the user decides to name their file.

He shouldn't be looking for a specific character case, he should make it case-insensitive. It's less work and better coding practice.


* - Love2D - Jun 22, 2012

(Jun 22, 2012 11:18 PM)Phizo Wrote:  He shouldn't be looking for a specific character case, he should make it case-insensitive. It's less work and better coding practice.

Yeah, he should, hopefully he sees your recommendation.

Dear DAVE,

Why have you suddenly stopped putting | before and after your thread title?


* - DAVEologist - Jun 23, 2012

(Jun 22, 2012 11:18 PM)Phizo Wrote:  He shouldn't be looking for a specific character case, he should make it case-insensitive. It's less work and better coding practice.

Let me try that. THanks. :)

(Jun 22, 2012 11:28 PM)Love2D Wrote:  Yeah, he should, hopefully he sees your recommendation.

Dear DAVE,

Why have you suddenly stopped putting | before and after your thread title?

And...I only use || for threads that needs importance in viewing. It's a rather neat way of attracting posters when I need some serious help.

And thanks for the help everyone. :)


* - Eight - Jun 23, 2012

(Jun 22, 2012 05:51 PM)Love2D Wrote:  Well if you're just going to check the file extension, you could use strrchr.

PHP Code

<?php 
$d 
= strrchr(strtolower($_FILES["file"]["name"][$c]), '.'); 

Personally I use pathinfo :
PHP Code

<?php 
$ext 
= pathinfo($file, PATHINFO_EXTENSION); 
But strrchr is also working. Except it's harder to pronounce 😏