Jump to content

Help with image name


Riparian

Recommended Posts

Hi

 

I have just discovered a problem that for months I could not identify in my error logs.

 

The logs contain 100s of errors like the following

Warning: imagesx(): supplied argument is not a valid Image resource in C:\Server\Apache........on line 43

Warning: imagesy(): supplied argument is not a valid Image resource in C:\Server\Apache.......on line 44

 

etc etc

 

The error seems to stem from people uploading  files e.g.  file.to.upload.jpg because the createthumb functions contaings preg_match('/\.(\w+)/',$name,$system)

 

On the other hand ...........  file_to_upload is no problem

 

Does someone have a fix for removing all extraneous joining characters Except the one before the image type identifier ?

 

Cheers and any help is greatly appreciated.

p.s. I have a hard time with regular expressions

 

Link to comment
Share on other sites

Does someone have a fix for removing all extraneous joining characters Except the one before the image type identifier ?

This may not be the best way, but this is what came to mind:

<?php
function fixpic($name){
$ray = explode('.', $name);
$ltag = '_'.$ray[count($ray)-1];
$rtag = '.'.$ray[count($ray)-1];
$name = implode('_', $ray);
return(str_replace($ltag, $rtag, $name));
}



$name = 'file.to.upload.jpg';
$new = fixpic($name);
echo $new;
?>

Link to comment
Share on other sites

Does someone have a fix for removing all extraneous joining characters Except the one before the image type identifier ?

 

It might be more productive to show the code which is causing the error. You are getting an error that the resource is not a valid image. Based upon what you have stated I think the problem is that you have copied the temp image and changed the name in the process but you are using a different value when trying to reference the image for some other purposes. So, I don't think the problem is due to how you are changing the name, but that you aren't using the new name in every place that you need to.

Link to comment
Share on other sites

Thank you for the replies and sunfighter is on the mark.

 

If it helps others in the future I have solved the problem by counting the periods in the name, replacing them with '' and then re-naming the $_FILES['upload']['name'] BEFORE using move_uploaded_file()

 

e.g.

//check  if more that one period in the file name

$needle='.';

$haystack=$_FILES['upload']['name'];

$count = strlen($haystack) - strlen(str_replace(str_split($needle), '', $haystack));

 

 

 

if($count>1){

$string = $haystack;

$finalPeriod = strrpos($string, ".");

$string = str_replace(".", "", substr($string, 0, $finalPeriod )) . substr($string, $finalPeriod );

$_FILES['upload']['name']=$string;

}

 

//echo $_FILES['upload']['name'];

 

Seems to work fine

 

Cheers and thanks

Link to comment
Share on other sites

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.