无法将带有php的图像文件上传到服务器

时间:2015-09-18 08:58:53

标签: php html sql

我已经在html中创建了一个表单,对于上传图片,它看起来很完美,因为当我上传图片时,它会给我字符串"成功",但我无法找到服务器中的文件。

这是我的代码:

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

      $expensions= array("jpeg","jpg","png");

      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
        }

      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"./photo/".$file_name);
         echo "Success";
      }
      else{
         print_r($errors);
      }
   }
?>

       <form action="" method="POST" enctype="multipart/form-data">
     <input type="file" name="image" />
     <input type="submit"/>

     <ul>
        <li>Sent file: <?php echo $_FILES['image']['name'];  ?>
        <li>File size: <?php echo $_FILES['image']['size'];  ?>
        <li>File type: <?php echo $_FILES['image']['type'] ?>
     </ul>

  </form>

2 个答案:

答案 0 :(得分:1)

检查move_uploaded_file:

$ret_value = move_uploaded_file($file_tmp,"./photo/".$file_name);

if ($ret_value == false) die ("Ups! Couldn't actually move the temp file! No success at all.");

如果返回false(错误),则无法从临时文件中实际移动文件,您仍然可以获得&#34;成功&#34;消息,因为没有例外。

如果您收到错误,可以通过将文件移动到实际的现有目录来修复错误。

move_uploaded_file($file_tmp,"./poi/photo/".$file_name);

答案 1 :(得分:0)

您的图片上传正在清除每次验证。

但是,你没有把它存放在正确的路径上:

变化:

private List<XMLTransaction> GetAllTransForStage(List<XElement> allTransDetails, string path, int curStage, int nxtStage, int journeyEnd, int IDStage, int IDJourney, int IDDuty, int IDModule, string BoardingStages)
    {
        var end = nxtStage > 0 ? nxtStage : journeyEnd;
        var doc = XDocument.Load(path);
        List<XElement> StageRersult = GetTranseDetails(doc);
        var transForCurrentStage = StageRersult.Where(s => Int32.Parse(s.Attribute("Position").Value.ToString()) > curStage && Int32.Parse(s.Attribute("Position").Value.ToString()) < end).ToList();
        var transactions = new List<XMLTransaction>();


        string TripBal = "";
        string serial = "";
        int IDTranse = 0;
        int hexClassValue = 0;
        int second = 0;
        double seconds = 0;
        TimeSpan t;
        string formatedTime = "";
        string date = "";
        string issueDate = "";
        string issueTime = "";
        int stageno = 0;
        int HexClasValue = 0;
        int Fare = 0;

        DataTable dt4 = new DataTable();
        dt4.Columns.AddRange(new DataColumn[17] {
                new DataColumn("id_Trans",typeof(int)),
                new DataColumn("id_Stage", typeof(int)),
                new DataColumn("id_Journey", typeof(int)),
                new DataColumn("id_Duty",typeof(int)),
                new DataColumn("id_Module",typeof(int)),
                new DataColumn("int2_BoardingStageID",typeof(int)),
                new DataColumn("int2_AlightingStageID",typeof(int)),
                new DataColumn("int2_Class",typeof(int)),
                new DataColumn("int4_Revenue",typeof(int)),
                new DataColumn("int4_NonRevenue",typeof(int)),
                new DataColumn("int2_TicketCount",typeof(int)),
                new DataColumn("int2_PassCount",typeof(int)),
                new DataColumn("int2_Transfers",typeof(int)),
                new DataColumn("dat_TransDate",typeof(DateTime)),
                new DataColumn("dat_TransTime",typeof(DateTime)),
                new DataColumn("str_SerialNumber",typeof(string)),
                new DataColumn("int4_TripBal",typeof(int))
            });


        int batchID = Convert.ToInt32(dtImportDateTime.ToString("yyyyMMdd"));
        XDocument xdoc3 = XDocument.Load(path);
        XDocument xdoc4 = XDocument.Load(path);
        //////////////
        string ConString = Settings.Default.ebusimporterConnectionString;
        using (SqlConnection con = new SqlConnection(ConString))
        {

            string sql = "SELECT TOP 1 id_Trans FROM Trans ORDER BY 1 DESC";
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                con.Open();

                object val = cmd.ExecuteScalar();
                if (val != null)
                {
                    IDTranse = Convert.ToInt32(val);
                    IDTranse = IDTranse + 1;
                }
                else if (val == null)
                {
                    IDTranse = Convert.ToInt32(val);
                    IDTranse = IDTranse + 1;
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }


        foreach (var tran in transForCurrentStage)
        {


            string Hex = tran.Element("TicketType").Value.ToString();
            string stageN = tran.Element("StageNo").Value.ToString();
            string Issuetime = tran.Element("IssueTime").Value.ToString();
            string fares = tran.Element("Fare").Value.ToString();
            string Issuedate = tran.Element("IssueDate").Value.ToString();
            string TicketSerialNo = tran.Element("TicketSerialNo").Value.ToString();





            //  string TicketType = item.TicketType.ToString();

            hexClassValue = int.Parse(Hex, NumberStyles.HexNumber);
            stageno = Int32.Parse(stageN);
            HexClasValue = hexClassValue;
            second = Int32.Parse(Issuetime);
            seconds = second;
            t = TimeSpan.FromSeconds(seconds);
            formatedTime = string.Format("{0:D2}:{1:D2}:{2:D2}",
                      t.Hours,
                      t.Minutes,
                       t.Seconds);
            Fare = Int32.Parse(fares);
            string dateLength = Issuedate;

            if (dateLength.Length == 5 && HexClasValue == 17 || HexClasValue == 18 || HexClasValue == 33 || HexClasValue == 34 || HexClasValue == 65 || HexClasValue == 66)
            {
                date = "0" + Issuedate;
                issueDate = DateTime.ParseExact(date, "dMMyy", null).ToString("yyyy-MM-d");
                issueTime = issueDate + " " + formatedTime;

                //Cash
                if (HexClasValue == 17 || HexClasValue == 18 || HexClasValue == 33 || HexClasValue == 34 || HexClasValue == 65 || HexClasValue == 66)
                {

                    dt4.Rows.Add(IDTranse, IDStage, IDJourney, IDDuty, IDModule, Convert.ToInt16(BoardingStages), Convert.ToInt16(stageN), Convert.ToInt16(HexClasValue), Fare, 0, 1, 0, 0, Convert.ToDateTime(issueDate), Convert.ToDateTime(issueTime), TicketSerialNo, Convert.ToInt32(0));
                    IDTranse++;

                }
            }

            if (dateLength.Length == 6 && HexClasValue == 17 || HexClasValue == 18 || HexClasValue == 33 || HexClasValue == 34 || HexClasValue == 65 || HexClasValue == 66)
            {
                date = Issuedate;
                issueDate = DateTime.ParseExact(date, "dMMyy", null).ToString("yyyy-MM-d");
                issueTime = issueDate + " " + formatedTime;

                //Cash
                if (HexClasValue == 17 || HexClasValue == 18 || HexClasValue == 33 || HexClasValue == 34 || HexClasValue == 65 || HexClasValue == 66)
                {

                    dt4.Rows.Add(IDTranse, IDStage, IDJourney, IDDuty, IDModule, Convert.ToInt16(BoardingStages), Convert.ToInt16(stageN), Convert.ToInt16(HexClasValue), Fare, 0, 1, 0, 0, Convert.ToDateTime(issueDate), Convert.ToDateTime(issueTime), TicketSerialNo, Convert.ToInt32(0));
                    IDTranse++;

                }
            }
        }

        string ConnString4 = Settings.Default.ebusimporterConnectionString;
        if (HexClasValue == 11000)
        {

        }
        else if (dt4.Rows.Count != 0)
        {

            using (SqlConnection con4 = new SqlConnection(ConnString4))
            {
                using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con4))
                {

                    //revenuebal  = 0 
                    sqlBulkCopy.DestinationTableName = "dbo.Trans";

                    sqlBulkCopy.ColumnMappings.Add("id_Trans", "id_Trans");
                    sqlBulkCopy.ColumnMappings.Add("id_Stage", "id_Stage");
                    sqlBulkCopy.ColumnMappings.Add("id_Journey", "id_Journey");
                    sqlBulkCopy.ColumnMappings.Add("id_Duty", "id_Duty");
                    sqlBulkCopy.ColumnMappings.Add("id_Module", "id_Module");
                    sqlBulkCopy.ColumnMappings.Add("int2_BoardingStageID", "int2_BoardingStageID");
                    sqlBulkCopy.ColumnMappings.Add("int2_AlightingStageID", "int2_AlightingStageID");
                    sqlBulkCopy.ColumnMappings.Add("int2_Class", "int2_Class");
                    sqlBulkCopy.ColumnMappings.Add("int4_Revenue", "int4_Revenue");
                    sqlBulkCopy.ColumnMappings.Add("int4_NonRevenue", "int4_NonRevenue");
                    sqlBulkCopy.ColumnMappings.Add("int2_TicketCount", "int2_TicketCount");
                    sqlBulkCopy.ColumnMappings.Add("int2_PassCount", "int2_PassCount");
                    sqlBulkCopy.ColumnMappings.Add("int2_Transfers", "int2_Transfers");
                    sqlBulkCopy.ColumnMappings.Add("dat_TransDate", "dat_TransDate");
                    sqlBulkCopy.ColumnMappings.Add("dat_TransTime", "dat_TransTime");
                    sqlBulkCopy.ColumnMappings.Add("str_SerialNumber", "str_SerialNumber");
                    sqlBulkCopy.ColumnMappings.Add("int4_TripBal", "int4_TripBal");

                    con4.Open();
                    sqlBulkCopy.WriteToServer(dt4);
                    con4.Close();

                }
            }
        }
        LogwriterSuccess("ImportTrans", path);

        return transactions;
    }

要:

move_uploaded_file($file_tmp,"./photo/".$file_name);

此外,对目录move_uploaded_file($file_tmp,"./poi/photo/".$file_name); 应用写入权限。