Jump to content

Recommended Posts

Trying to set up an error message when someone tries to upload a file without the approved .ext.

I have it working so it won't up load but I am trying to get an error to print out.

 

I was thinking that I could do something like this

 

if the ext are wrong set

$errorMsg1 == 1;

and then the page will refresh and I would pass that variable to echo out

 

if ($errorMsg1 == 1){
echo "Invalid"; }
else {

 

but it isn't passing can anyone help me with this? Tyring to keep it simple

 

this is the code to select an image

else{

  $result = mysql_query("SELECT * FROM photos WHERE userID LIKE '$clientID'");
  while ($r=mysql_fetch_array($result))
  {
     $photo_1=$r['photo_1'];
     $photo_2=$r['photo_2'];
     $photo_3=$r['photo_3'];
     $photo_4=$r['photo_4'];
     $photo_5=$r['photo_5'];

   echo " 
    <form enctype='multipart/form-data' action='' method='POST'>
      <input type='hidden' name='MAX_FILE_SIZE' value='500000' />
      <div id='imageTop'>Image &#35;1</div>
        ";
        if ($errorMsg1 == 1){
       echo "Invalid"; }
       echo "

       <div id='imageBottom'>
         <span class='image'>";
           if (empty($photo_1)) { 
            echo " <img src='uploads/noPhoto.gif' width='75' height='75' class='zip'> ";
              }
            else {
             echo "<a href='uploads/$photo_1' ><img src='uploads/$photo_1' width='75' height='75' class='zip'></a> ";
              }
            echo " 
         </span>
         <span class='action'>
             <input type='file' name='photo_1' class='zip'><br><br>
            <input type='checkbox' name='delete_1'>Select to Delete image
          </span>
       </div>

}

 

 

this is the code of what to do with that image

  if (isset($_POST['delete_1']))  {
    $query = "UPDATE photos SET photo_1='' WHERE userID='$clientID'";
    $result = mysql_query($query) or die(mysql_error());
    echo " <div id='aboutUpdate'><img src='img/loader.gif'> Information is updating</div> ";
    echo "<meta http-equiv=refresh content=\"0; URL=photos.php\">";
  }
  else if ($one != NULL) {
    $extension = strrchr($_FILES['photo_1']['name'],'.');
    $extension = strtolower($extension);

    if($extension != '.jpg' && $extension != '.gif' && $extension != '.png' && $extension != '.bmp' ){
      $errorMsg1 == 1;
      echo "<meta http-equiv=refresh content=\"0; URL=photos.php\">";
    }
    else {
    $photoNumber="_1";
    $finalName="$clientID$photoNumber";
    $save_path = "uploads/";
    $target_path = $save_path . basename( $_FILES['photo_1']['name']); 
    $NewPhotoName = $finalName; 
    $withExt = $NewPhotoName . $extension;
    $filename = $save_path . $NewPhotoName . $extension; 
    if(move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename)) {
      $query = "UPDATE photos SET photo_1='$withExt' WHERE userID='$clientID'";
      $result = mysql_query($query) or die(mysql_error());
        echo " <div id='aboutUpdate'><img src='img/loader.gif'> Information is updating</div> ";
        echo "<meta http-equiv=refresh content=\"0; URL=photos.php\">";
      }
    }
    else  {

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/
Share on other sites

Can you not pass it via the url?

 

Assuming you are calling a php file to do the upload when you return to the page which contains the upload photo form you can pass the message via the URL.

 

Upload photo

//Close the connection to the database	
mysql_close($conn);
header("Location: add_school_photo_form.php? message={$message}&schoolid={$schoolid}");
exit();

 

Form that contains the upload photo form

if ( isset( $_GET['message'] ) )
{
    echo $_GET['message'];
    echo '<br />';
    echo '<br />';
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125006
Share on other sites

Without looking but can you not adapt your

echo "<meta http-equiv=refresh content=\"0; URL=photos.php\">";

to include the variable to the end in the same way as you would do it if it was on a seperate page.

 

If not a crude solution would be to set a session variable and then null it once you have outputted it...

 

 

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125010
Share on other sites

The only way I can get it to pass a variable to the url is by setting and including the variable right before the url like below, which of course won't do

 

$errorMsg1=1;
echo "<meta http-equiv=refresh content=\"0; URL=photos.php?errorMsg1=$errorMsg1 \">";

 

 

This is where the variable needs to be set and then passed to the url at the end.

if( isset($_POST['submitPhoto']))
{
  if (isset($_POST['delete_1']))  {
    $query = "UPDATE photos SET photo_1='' WHERE userID='$clientID'";
    $result = mysql_query($query) or die(mysql_error());
    echo " <div id='aboutUpdate'><img src='img/loader.gif'> Information is updating</div> ";
    echo "<meta http-equiv=refresh content=\"0; URL=photos.php\">";
  }
  else {
    $extension = strrchr($_FILES['photo_1']['name'],'.');
    $extension = strtolower($extension);

    if($extension != '.jpg' && $extension != '.gif' && $extension != '.png' && $extension != '.bmp' ){
      $errorMsg1 == 1;
    }

    else {
      $photoNumber="_1";
      $finalName="$clientID$photoNumber";
      $save_path = "uploads/";
      $target_path = $save_path . basename( $_FILES['photo_1']['name']); 
      $NewPhotoName = $finalName; 
      $withExt = $NewPhotoName . $extension;
      $filename = $save_path . $NewPhotoName . $extension; 
        if(move_uploaded_file($_FILES['photo_1']['tmp_name'], $filename)) {
        $query = "UPDATE photos SET photo_1='$withExt' WHERE userID='$clientID'";
        $result = mysql_query($query) or die(mysql_error());
        echo " <div id='aboutUpdate'><img src='img/loader.gif'> Information is updating</div> ";
      }
    }

  echo "<meta http-equiv=refresh content=\"0; URL=photos.php?errorMsg1=$errorMsg1 \">";

}

 

Hoping to end up with a url like such

 

echo "<meta http-equiv=refresh content=\"0; URL=photos.php?errorMsg1=$errorMsg1&errorMsg2=$errorMsg2 \">";

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125218
Share on other sites

You aren't assigning the value to the $errorMsg1 variable properly.

 

if($extension != '.jpg' && $extension != '.gif' && $extension != '.png' && $extension != '.bmp' ){
      $errorMsg1 == 1;  // Using comparison operator instead of assignment operator here . . .

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125260
Share on other sites

I did catch that but it still won't work.

 

What I get when I echo them out, file I used was test.tif

 

Extension .tif

Execution reaches here :1

 

  else { 
    $extension = strrchr($_FILES['photo_1']['name'],'.'); 
    $extension = strtolower($extension); 

    if($extension != '.jpg' && $extension != '.gif' && $extension != '.png' && $extension != '.bmp' ){ 
  $errorMsg1 = 1; 
  echo "Extension $extension <br>"; 
  echo 'Execution reaches here :'.$errorMsg1 = 1; 

    } 

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125262
Share on other sites

Using a file named test.tif, that's where the execution should stop. You're telling the conditional statement "If the file extension isn't jpg AND isn't gif AND isn't png AND isn't bmp:  echo "Extension $extension <br>";  echo 'Execution reaches here :'.$errorMsg1 = 1;"

 

Otherwise, if the extension IS one of those listed, skip to else{} instead.

 

A .tif extension will execute the mail part of the conditional and will not make it to the else{}.

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125272
Share on other sites

Pikachu2000 that is correct and what I am going for.

 

So once it has determined that the file is not an acceptable one I am having it set $errorMsg1 = 1, which all works. Then I want to pass that into my url but cannot.

 

I think the issue comes with my url, perhaps I cannot pass a variable into a url that is being used with the refresh?

echo "<meta http-equiv=refresh content=\"0; URL=photos.php?errorMsg1=$errorMsg1\">";

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125283
Share on other sites

Ah, now it's clearer. That should work like that, although there's probably a better way to do it. Is it even redirecting at all? I just set up a test locally, and it worked, adding the get var to the URL in the meta refresh tag.

Link to comment
https://forums.phpfreaks.com/topic/216511-passing-a-variable/#findComment-1125291
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.