Jump to content

upload images with php


luismc2007
Go to solution Solved by luismc2007,

Recommended Posts

Hi: I´m trying to make my own code for upload images using php to my server for my webpage, but I don´t know why the code doesn´t work at 100%.

I know that its works until a point but not more away...

Can you help me??

 

I have two files: an html and php called "form.html" and "sube.php" that I post here...

And now explain what I´m try to do...

 

[ File: form.html ]

 

<form action="sube.php" method="post" enctype="multipart/form-data">
Sube un archivo:<br />
<input type="file" name="archivo" id="archivo" />
<br  />
<input type="submit" name="boton" value="Subir" />
</form>

 

[ File: sube.php ]

 

<?php
echo "Name of image: ".$_FILES["archivo"]["name"];
echo "<br />";

$tamano = $_FILES["archivo"]["size"];
echo "Size of image: ".$_FILES["archivo"]["size"]; // here always an error
echo "<br>";

$t_max="50000000000";
echo "Maximum size:";
echo "$t_max <br>";

if( $tamano < $t_max)
{
$destino = "fotogal/grandes";
$sep=explode('image/',$_FILES["archivo"]["type"]);

if($_FILES["archivo"]["type"] == "gif" || $_FILES["archivo"]["type"] == "jpg" )
 move_uploaded_file ( $_FILES["archivo"]["tmp_name"], $destino . '/' .$_FILES["archivo"]["name"]);
 }
else echo "No way..."; // OUT
}
else
 {
 echo "too much weight of image.";
 }
 

?>

 

The case of this is that always i reach the same point and don´t know how to go further...

The point is that always I receive:

Name.."ok"...

Size: This item is completely different of the weight of the image

And...

No way.

 

This is always my result.

 

Can you help me with this code????? I would like to upload my own images, but I´m not able to do it... and don´t know why.

 

Thanks a lot.

regards

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

There are erors in your sube.php file:

 

Try this:

 

sube.php

<?php

echo "Name of image: " . $_FILES["archivo"]["name"];
echo "<br />";

$tamano = $_FILES["archivo"]["size"];
echo "Size of image: " . $_FILES["archivo"]["size"]; // here always an error
echo "<br>";

$t_max = "50000000000";
echo "Maximum size:";
echo "$t_max <br>";

echo "File type:";
echo  $_FILES["archivo"]["type"].'<br />';

if ($_FILES['archivo']['error'] === 0) {
    if ($tamano < $t_max) {
        
        $destino = "fotogal/grandes";
        
        //get a file extension
        $file_ext = basename($_FILES["archivo"]['name']);
        
        // check the file extension in php
        $pattern = '/^.*\.(jpg|jpeg|gif)$/i';
        
        if (!preg_match($pattern, $file_ext)) {
                echo "ERROR: You must select a valid image file!"; exit;
            }

        if(move_uploaded_file($_FILES["archivo"]["tmp_name"], $destino . '/' . $_FILES["archivo"]["name"])){
            echo "Upload success";
         } else {
             echo 'Upload failed.'; 
         } 
    } else {

        echo "too much weight of image.";
    }
} else {
    echo "ERROR: Some error(s) was finded in that file";     
}
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.