Jump to content

combine 2x php files into 1


jacko_162

Recommended Posts

I have a small form which executes a php file, and the executable php file.

 

is there anyway to have it just using the 1 php file?

 

form code:

 <form enctype="multipart/form-data" action="upload-exec.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form> 

 

uploader.php

<?php 
$target = "upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our size condition 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 
?>

 

i did try using:

if (isset($_POST['submit'])) {

 

with no luck.

 

Many thanks

Link to comment
Share on other sites

<?php

if(isset($_FILES['uploaded']['name']))
{
    $target = "upload/";
    $fileName = basename($_FILES['uploaded']['name']);
    $target = $target . $fileName;
    $errors = array();

    //This is our size condition
    if ($uploaded_size > 350000)
    {
         $errors[] = "Your file is too large.";
    }
     //This is our limit file type condition
    if ($uploaded_type=="text/php")
    { 
        $errors[] = "No PHP files";
    }

    //Here we check that no validation errors have occured
    if (count($errors)==0)
    {
        //Try to upload it
        if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
        {
            $errors[] = "Sorry, there was a problem uploading your file.";
        }
    }
    
    //If no errors show confirmation message
    if(count($errors)==0)
    {
        echo "The file {$fileName} has been uploaded";
    }
    else
    {
        //show error message
        echo "Sorry your file was not uploaded due to the following errors:<br>\n";
        echo "<ul>\n";
        foreach($errors as $error)
        {
            echo "<li>{$error}</li>\n";
        }
        echo "</ul>\n";
    }
}
else
{
    //Show the form
    echo "<form enctype='multipart/form-data' action='' method='POST'>\n";
    echo "Please choose a file: <input name='uploaded' type='file' /><br />\n";
    echo "<input type='submit' value='Upload' />\n";
    echo "</form>\n"; 
}
?>

Link to comment
Share on other sites

<?php

if(isset($_FILES['uploaded']['name']))
{
    $target = "upload/";
    $fileName = basename($_FILES['uploaded']['name']);
    $target = $target . $fileName;
    $errors = array();

    //This is our size condition
    if ($uploaded_size > 350000)
    {
         $errors[] = "Your file is too large.";
    }
     //This is our limit file type condition
    if ($uploaded_type=="text/php")
    { 
        $errors[] = "No PHP files";
    }

    //Here we check that no validation errors have occured
    if (count($errors)==0)
    {
        //Try to upload it
        if(!move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
        {
            $errors[] = "Sorry, there was a problem uploading your file.";
        }
    }
    
    //If no errors show confirmation message
    if(count($errors)==0)
    {
        echo "The file {$fileName} has been uploaded";
    }
    else
    {
        //show error message
        echo "Sorry your file was not uploaded due to the following errors:<br>\n";
        echo "<ul>\n";
        foreach($errors as $error)
        {
            echo "<li>{$error}</li>\n";
        }
        echo "</ul>\n";
    }
}
else
{
    //Show the form
    echo "<form enctype='multipart/form-data' action='' method='POST'>\n";
    echo "Please choose a file: <input name='uploaded' type='file' /><br />\n";
    echo "<input type='submit' value='Upload' />\n";
    echo "</form>\n"; 
}
?>

 

Code works loverly, how can i add a query in your above code to add the filename to the database and if possible can we generate a unique random number and rename said file to stop other files being overwritten?

Link to comment
Share on other sites

Well, you have to decide: should you copy the file first and then update the database or update the database first then move the file. I'd probably go with the former. Just add the code necessary as an else condition to the if() statement that uploads the file.

 

As for adding a unique random number you would either have to check the available file names or check the file names stored in the database. Again, I would go with the former. I'd do a single check to see if there is an exact match of the file name. If no, move the file with the original name. If yes, they I would use glob() to find all the "like" file names. You would need to split the file name into the path/name part and the extension. Then find all the files matching the original with additional "text" something like

$matches = glob($path.$name.'*'.$ext);

Then just loop through till you find an available number to use.

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.