Jump to content

Recommended Posts

ok i added this script in the bottom of my other uploader script.

what i wanted to do was, to upload an imagefile to an folder called uploads

and in the same time add the name of the file to an database called uploads,

together with the next id of another database

 

i dont get any error msg, the page is just blank.

<?php
include 'sqlconnect.php';

$result = mysql_query("
    SHOW TABLE STATUS LIKE 'aktiviteter'
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];

 
$sql = sprintf(
       "INSERT INTO aktiviteter
            (`title`, `firma`, `beskrivelse`, `information`, `pris`, `varighed`, `antal`,
             `adresse`, `by`, `postnummer`, `telefon`, `email`, `hjemmeside`)
        VALUES
            ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
        mysqli_real_escape_string($con, $_POST['title']),
        mysqli_real_escape_string($con, $_POST['firma']),
        mysqli_real_escape_string($con, $_POST['beskrivelse']),
        mysqli_real_escape_string($con, $_POST['information']),
        mysqli_real_escape_string($con, $_POST['pris']),
        mysqli_real_escape_string($con, $_POST['varighed']),
        mysqli_real_escape_string($con, $_POST['antal']),
        mysqli_real_escape_string($con, $_POST['adresse']),
        mysqli_real_escape_string($con, $_POST['by']),
        mysqli_real_escape_string($con, $_POST['postnummer']),
        mysqli_real_escape_string($con, $_POST['telefon']),
        mysqli_real_escape_string($con, $_POST['email']),
        mysqli_real_escape_string($con, $_POST['hjemmeside'])
        );
 
if (!mysqli_query($con, $sql))
{
    die('Error: ' . mysqli_error($con));
}
echo "Aktiviteten er uploaded";



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

$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_filesize = 10485760;
$upload_path = 'uploads/';
$targetId = $next_increment

$filename = $_FILES['billede']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

if(!in_array($ext,$allowed_filetypes))
  die('The file you attempted to upload is not allowed.');

if(filesize($_FILES['billede']['tmp_name']) > $max_filesize)
  die('The file you attempted to upload is too large.');

if(!is_writable($upload_path))
  die('You cannot upload to the specified directory, please CHMOD it to 777.');

if(move_uploaded_file($_FILES['billede']['tmp_name'],$upload_path . $filename)) {
   $query = "INSERT INTO uploads (name, target) VALUES ($filename, $targetId)"; 
   mysql_query($query);

echo 'Your file upload was successful!';


} else {
     echo 'There was an error during the file upload.  Please try again.';
}
}
 
mysqli_close($con);
Link to comment
https://forums.phpfreaks.com/topic/290613-uploading-file/
Share on other sites

your code produces a fatal php parse/syntax error due to a missing ;. i'm not going to tell you where, because your development system should have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini so that all php errors will be reported and displayed. for this kind of error, php can point you to the correct area of the code (usually the actual problem causing a syntax error is immediately prior to where the syntax error is reported) and you should be able to find things like missing punctuation.

 

edit: next you have a functional problem in getting the current autoincrement value from a table, increment it, and using it. if you have concurrent requests to your page, you will end up with wrong values. the way you would do this is, after the INSERT query runs for the aktiviteter table, use mysqli_insert_id() to get the actual id that was generated.

 

next, you are mixing mysql_ (no i) and mysqli_ (with an i) functions. you need to use all mysqli_ (with and i) functions.

Link to comment
https://forums.phpfreaks.com/topic/290613-uploading-file/#findComment-1488701
Share on other sites

the file doesn't get uploaded

 

 

how do you know that? what sort of symptom or error do you get?

 

there's a dozen different things that could prevent an upload from working, starting with the form not having the needed enctype attribute all the way through to file/folder permissions preventing move_uploaded_file() from being able to move the file to the destination folder. is your code even testing if the file got uploaded without any errors, before trying to use the uploaded file information?

Link to comment
https://forums.phpfreaks.com/topic/290613-uploading-file/#findComment-1488760
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.